Example #1
0
 def decode(self, file, filename):
     try:
         reader = pypng.Reader(file=file)
         width, height, pixels, metadata = reader.asDirect()
     except Exception, e:
         raise ImageDecodeException(
             'PyPNG cannot read %r: %s' % (filename or file, e))
Example #2
0
    def decode(self, filename, file):
        if not file:
            file = open(filename, 'rb')

        try:
            reader = pypng.Reader(file=file)
            width, height, pixels, metadata = reader.asDirect()
        except Exception as e:
            raise ImageDecodeException('PyPNG cannot read %r: %s' %
                                       (filename or file, e))

        if metadata['greyscale']:
            if metadata['alpha']:
                fmt = 'LA'
            else:
                fmt = 'L'
        else:
            if metadata['alpha']:
                fmt = 'RGBA'
            else:
                fmt = 'RGB'
        pitch = len(fmt) * width

        pixels = array.array('BH'[metadata['bitdepth'] > 8],
                             itertools.chain(*pixels))
        return ImageData(width, height, fmt, pixels.tobytes(), -pitch)