def _get_band_images(channel_data, channel_ids, color_mode, size, depth): bands = {} for channel, channel_id in zip(channel_data, channel_ids): pil_band = _channel_id_to_PIL(channel_id, color_mode) if pil_band is None: warnings.warn("Unsupported channel type (%d)" % channel_id) continue if channel.compression in [Compression.RAW, Compression.ZIP, Compression.ZIP_WITH_PREDICTION]: if depth == 8: im = _from_8bit_raw(channel.data, size) elif depth == 16: im = _from_16bit_raw(channel.data, size) elif depth == 32: im = _from_32bit_raw(channel.data, size) else: warnings.warn("Unsupported depth (%s)" % depth) continue elif channel.compression == Compression.PACK_BITS: if depth != 8: warnings.warn("Depth %s is unsupported for PackBits compression" % depth) continue im = frombytes('L', size, channel.data, "packbits", 'L') else: if Compression.is_known(channel.compression): warnings.warn("Compression method is not implemented (%s)" % channel.compression) else: warnings.warn("Unknown compression method (%s)" % channel.compression) continue bands[pil_band] = im.convert('L') return bands
def _decompress_channel(channel, depth, size): if channel.compression in (Compression.RAW, Compression.ZIP, Compression.ZIP_WITH_PREDICTION): if depth == 8: im = _from_8bit_raw(channel.data, size) elif depth == 16: im = _from_16bit_raw(channel.data, size) elif depth == 32: im = _from_32bit_raw(channel.data, size) else: warnings.warn("Unsupported depth (%s)" % depth) return None elif channel.compression == Compression.PACK_BITS: if depth != 8: warnings.warn("Depth %s is unsupported for PackBits compression" % depth) im = frombytes('L', size, channel.data, "packbits", 'L') else: if Compression.is_known(channel.compression): warnings.warn("Compression method is not implemented " "(%s)" % channel.compression) else: warnings.warn("Unknown compression method (%s)" % channel.compression) return None return im.convert('L')
def _channels_data_to_PIL(channels_data, channel_types, size, depth, icc_profile): if Image is None: raise Exception("This module requires PIL (or Pillow) installed.") if size == (0, 0): return bands = {} for channel, channel_type in zip(channels_data, channel_types): pil_band = channel_id_to_PIL(channel_type) if pil_band is None: warnings.warn("Unsupported channel type (%d)" % channel_type) continue if channel.compression in [Compression.RAW, Compression.ZIP, Compression.ZIP_WITH_PREDICTION]: if depth == 8: im = _from_8bit_raw(channel.data, size) elif depth == 16: im = _from_16bit_raw(channel.data, size) elif depth == 32: im = _from_32bit_raw(channel.data, size) else: warnings.warn("Unsupported depth (%s)" % depth) continue elif channel.compression == Compression.PACK_BITS: if depth != 8: warnings.warn("Depth %s is unsupported for PackBits compression" % depth) continue im = frombytes('L', size, channel.data, "packbits", 'L') else: if Compression.is_known(channel.compression): warnings.warn("Compression method is not implemented (%s)" % channel.compression) else: warnings.warn("Unknown compression method (%s)" % channel.compression) continue bands[pil_band] = im.convert('L') mode = _get_mode(bands.keys()) merged_image = Image.merge(mode, [bands[band] for band in mode]) if mode == 'CMYK': # invert CMYK data merged_image = frombytes('CMYK', size, merged_image.tobytes(), 'raw', 'CMYK;I') # convert with inner profile, better brightness merged_image = merged_image.convert('RGB') elif icc_profile is not None: display_profile = ImageCms.createProfile('sRGB') # XXX: ImageCms.get_display_profile()? ImageCms.profileToProfile(merged_image, icc_profile, display_profile, inPlace=True) return merged_image
def _channels_data_to_PIL(channels_data, channel_types, size, depth, icc_profile): if Image is None: raise Exception("This module requires PIL (or Pillow) installed.") if size == (0, 0): return bands = {} for channel, channel_type in zip(channels_data, channel_types): pil_band = channel_id_to_PIL(channel_type) if pil_band is None: warnings.warn("Unsupported channel type (%d)" % channel_type) continue if channel.compression in [Compression.RAW, Compression.ZIP, Compression.ZIP_WITH_PREDICTION]: if depth == 8: im = _from_8bit_raw(channel.data, size) elif depth == 16: im = _from_16bit_raw(channel.data, size) elif depth == 32: im = _from_32bit_raw(channel.data, size) else: warnings.warn("Unsupported depth (%s)" % depth) continue elif channel.compression == Compression.PACK_BITS: if depth != 8: warnings.warn("Depth %s is unsupported for PackBits compression" % depth) continue im = frombytes('L', size, channel.data, "packbits", 'L') else: if Compression.is_known(channel.compression): warnings.warn("Compression method is not implemented (%s)" % channel.compression) else: warnings.warn("Unknown compression method (%s)" % channel.compression) continue bands[pil_band] = im.convert('L') mode = _get_mode(bands.keys()) merged_image = Image.merge(mode, [bands[band] for band in mode]) if icc_profile is not None: display_profile = ImageCms.createProfile('sRGB') # XXX: ImageCms.get_display_profile()? ImageCms.profileToProfile(merged_image, icc_profile, display_profile, inPlace=True) return merged_image
def _decompress_pattern_channel(channel): depth = channel.depth size = (channel.rectangle[3], channel.rectangle[2]) if channel.compression in (Compression.RAW, Compression.ZIP, Compression.ZIP_WITH_PREDICTION): if depth == 8: im = _from_8bit_raw(channel.data.value, size) elif depth == 16: im = _from_16bit_raw(channel.data.value, size) elif depth == 32: im = _from_32bit_raw(channel.data.value, size) else: warnings.warn("Unsupported depth (%s)" % depth) return None elif channel.compression == Compression.PACK_BITS: if depth != 8: warnings.warn( "Depth %s is unsupported for PackBits compression" % depth) try: import packbits channel_data = packbits.decode(channel.data.value) except ImportError as e: warnings.warn("Install packbits (%s)" % e) channel_data = b'\x00' * (size[0] * size[1]) # Default fill except IndexError as e: warnings.warn("Failed to decode pattern (%s)" % e) channel_data = b'\x00' * (size[0] * size[1]) # Default fill # Packbit pattern tends not to have the correct size ??? padding = len(channel_data) - size[0] * size[1] if padding < 0: warnings.warn('Broken pattern data (%g for %g)' % ( len(channel_data), size[0] * size[1])) channel_data += b'\x00' * -padding # Append default fill padding = 0 im = frombytes('L', size, channel_data[padding:], "raw", 'L') else: if Compression.is_known(channel.compression): warnings.warn( "Compression method is not implemented " "(%s)" % channel.compression) else: warnings.warn( "Unknown compression method (%s)" % channel.compression) return None return im.convert('L')
def _get_band_images(layer, channel_data, channel_ids, color_mode, size, depth): bands = {} for channel, channel_id in zip(channel_data, channel_ids): im = None mask_im = None mask_size = None pil_band = _channel_id_to_PIL(channel_id, color_mode) if pil_band is None: warnings.warn("Unsupported channel type (%d)" % channel_id) continue if channel.compression in [ Compression.RAW, Compression.ZIP, Compression.ZIP_WITH_PREDICTION ]: if pil_band == 'M': mask_size = (channel.mask_data.width(), channel.mask_data.height()) if mask_size[0] == 0 or mask_size[1] == 0: # warnings.warn("Empty layer mask found on layer '%s', ignoring it." % layer.name) continue try: mask_im = _from_8bit_raw(channel.data, mask_size) except Exception as e: warnings.warn("Failed parsing layer mask! " + str(e)) continue elif depth == 8: im = _from_8bit_raw(channel.data, size) elif depth == 16: im = _from_16bit_raw(channel.data, size) elif depth == 32: im = _from_32bit_raw(channel.data, size) else: warnings.warn("Unsupported depth (%s)" % depth) continue elif channel.compression == Compression.PACK_BITS: if depth != 8: warnings.warn( "Depth %s is unsupported for PackBits compression" % depth) continue if pil_band == 'M': mask_size = (channel.mask_data.width(), channel.mask_data.height()) if mask_size[0] == 0 or mask_size[1] == 0: # warnings.warn("Empty layer mask found on layer '%s', ignoring it." % layer.name) continue try: mask_im = frombytes('L', mask_size, channel.data, "packbits", 'L') except Exception as e: warnings.warn("Failed parsing layer mask! " + str(e)) continue else: im = frombytes('L', size, channel.data, "packbits", 'L') else: if Compression.is_known(channel.compression): warnings.warn("Compression method is not implemented (%s)" % channel.compression) else: warnings.warn("Unknown compression method (%s)" % channel.compression) continue if pil_band == 'M': if layer_mask_callback(layer) == False: continue im = Image.new('L', size, 255) offset = (channel.mask_data.left - layer.left, channel.mask_data.top - layer.top) im.paste(mask_im, offset) bands[pil_band] = im.convert('L') return bands