def test_layer_record_channel_sizes(): layer_record = LayerRecord( left=0, top=0, right=100, bottom=120, channel_info=[ ChannelInfo(id=ChannelID.CHANNEL_0), ChannelInfo(id=ChannelID.USER_LAYER_MASK), ChannelInfo(id=ChannelID.REAL_USER_LAYER_MASK), ], mask_data=MaskData( left=20, top=20, right=80, bottom=90, real_left=10, real_top=10, real_right=90, real_bottom=100, )) channel_sizes = layer_record.channel_sizes assert len(channel_sizes) == 3 assert channel_sizes[0] == (100, 120) assert channel_sizes[1] == (60, 70) assert channel_sizes[2] == (80, 90)
def test_channel_image_data(): check_write_read(ChannelImageData(), layer_records=LayerRecords()) layer_records = LayerRecords( [LayerRecord(channel_info=[ ChannelInfo(id=0, length=18), ChannelInfo(id=-1, length=18), ])]) channel_data_list = ChannelDataList([ ChannelData(0, b'\xff' * 16), ChannelData(0, b'\xff' * 16), ]) check_write_read(ChannelImageData([channel_data_list]), layer_records=layer_records)
def test_channel_data_list(): channel_info = [ ChannelInfo(id=0, length=20), ChannelInfo(id=1, length=20), ChannelInfo(id=2, length=20), ChannelInfo(id=-1, length=20), ] channel_items = [ ChannelData(0, b'\x00' * 18), ChannelData(0, b'\x00' * 18), ChannelData(0, b'\x00' * 18), ChannelData(0, b'\x00' * 18), ] check_write_read(ChannelDataList(channel_items), channel_info=channel_info)
def test_layer_info(): check_write_read(LayerInfo()) layer_records = LayerRecords( [LayerRecord(channel_info=[ ChannelInfo(id=0, length=18), ChannelInfo(id=-1, length=18), ])]) channel_image_data = ChannelImageData( [ChannelDataList([ ChannelData(0, b'\xff' * 16), ChannelData(0, b'\xff' * 16), ])]) check_write_read(LayerInfo(1, layer_records, channel_image_data))
def test_channel_info(): check_write_read(ChannelInfo(id=0, length=1), version=1) check_write_read(ChannelInfo(id=0, length=1), version=2)
# pixel layer and content layer are required # background layer is optional if pixel_layer is None or content_layer is None: print("Error: Invalid PSD. PSD lacks device pixel layer or content smart object layer.") exit(1) if not content_layer.has_mask(): # create artifical mask from rectangular area of content layer left, top, right, bottom = content_layer.bbox width, height = content_layer.width, content_layer.height depth, version = psd.depth, psd.version # artificial channeldata, channelinfo, maskdata mimicking ones from iPhone11 data = b'\xff' * (width * height) channel_data = ChannelData(compression=Compression.RLE) channel_data.set_data(data, width, height, depth, version) channel_info = ChannelInfo(id=ChannelID.USER_LAYER_MASK, length=channel_data._length) mask_data = MaskData( top=top, left=left, bottom=bottom, right=right, background_color=0 ) else: # find mask channel info/data channel_index, channel_info = next(( (i, info) for i, info in enumerate(content_layer._record.channel_info) if info.id == ChannelID.USER_LAYER_MASK )) channel_data = content_layer._channels[channel_index]