def _channels_data_to_image(channels_data, mode, size, depth): if size == (0, 0): return w, h = size num_channels = mode.length assert depth == 8 assert len(channels_data) == num_channels total_size = w * h * num_channels image_bytes = array.array(str("B"), [0] * total_size) for index, channel in enumerate(channels_data): # zip and zip-with-prediction data is already decoded data = channel.data if channel.compression == Compression.PACK_BITS: data = packbits.decode(data) image_bytes[index::num_channels] = array.array(str("B"), data) pixels = get_pixel_array(image_bytes, w, h, mode.length) return LoadedImage(mode, w, h, pixels)
def decode_packbits(data, height, version): with io.BytesIO(data) as fp: bytes_counts = read_be_array(('H', 'I')[version - 1], height, fp) result = b'' for count in bytes_counts: result += packbits.decode(fp.read(count)) return result
def channel_decode(channel, depth, width, height): '''psd channel data to full list data [psd channel] -> [ [line] [line] ... ] ''' channel_data = packbits.decode( channel.data ) if channel.compression == Compression.PACK_BITS else channel.data channel_data = array_from_raw(channel_data, depth) bl_channel = numpy.reshape(channel_data, (height, width)) bl_channel = numpy.flipud(bl_channel) return bl_channel
def decompress_rle( data, # type: bytes shape, # type: Tuple[int, int] depth, # type: int version # type: int ): # type: (...) -> np.ndarray """ Decompress run length encoded data. {} """ output = packbits.decode(data, shape[0], shape[1], color_depth_size_map[depth], version) # Now pass along to the raw decoder to get a Numpy array return decompress_raw(output, shape, depth, version)
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 _channels_data_to_image(channels_data, mode, size, depth): if size == (0, 0): return num_channels = mode.length assert depth == 8 assert len(channels_data) == num_channels total_size = size[0]*size[1]*num_channels image_bytes = array.array(str("B"), [0]*total_size) for index, channel in enumerate(channels_data): data = channel.data # zip and zip-with-prediction data is already decoded if channel.compression == Compression.PACK_BITS: data = packbits.decode(data) image_bytes[index::num_channels] = array.array(str("B"), data) pixels = get_pixel_array(image_bytes, size[0], size[1], mode.length) return Image(pixels, mode)
def test_raw(): encoded = packbits.encode(b'123') assert encoded == b'\x02123' assert packbits.decode(encoded) == b'123'
def test_encode_long_raw(): data = b"12345678" * 17 encoded = packbits.encode(data) print(encoded) assert packbits.decode(encoded) == data
def test_encode_long_rle2(): data = b"1" * 127 encoded = packbits.encode(data) assert packbits.decode(encoded) == data
def test_encode_long(): data = b'1' * 128 + b'12345678' * 17 encoded = packbits.encode(data) assert packbits.decode(encoded) == data
def test_encode_long_raw(): data = b'12345678' * 17 encoded = packbits.encode(data) print(encoded) assert packbits.decode(encoded) == data
def test_encode_long_rle3(): data = b'1' * 128 encoded = packbits.encode(data) assert packbits.decode(encoded) == data
def test_encode_switching_rle(): encoded = packbits.encode(b'1122') assert packbits.decode(encoded) == b'1122'
def test_encode2(): encoded = packbits.encode(b'112112') assert packbits.decode(encoded) == b'112112'
def test_encode_single(): encoded = packbits.encode(b'X') assert encoded == b'\x00X' assert packbits.decode(encoded) == b'X'
def test_encode_decode(): encoded = packbits.encode(RESULT) decoded = packbits.decode(encoded) assert decoded == RESULT
def test_restart_rle(): data = b'1' * 127 + b'foo' encoded = packbits.encode(data) assert packbits.decode(encoded) == data
def test_encode_long_raw(): data = b'12345678' * 16 encoded = packbits.encode(data) assert packbits.decode(encoded) == data
def test_encode_long_raw2(): data = b'12345678' * 16 encoded = packbits.encode(data) assert packbits.decode(encoded) == data
def test_decode(): decoded = packbits.decode(DATA) assert decoded == RESULT
def test_raw(): encoded = packbits.encode(b"123") assert encoded == b"\x02123" assert packbits.decode(encoded) == b"123"
def test_encode2(): encoded = packbits.encode(b"112112") assert packbits.decode(encoded) == b"112112"
def test_restart_rle(): data = b"1" * 127 + b"foo" encoded = packbits.encode(data) assert packbits.decode(encoded) == data
def test_encode_single(): encoded = packbits.encode(b"X") assert encoded == b"\x00X" assert packbits.decode(encoded) == b"X"