cgohlke / psdtags

Read and write layered TIFF ImageSourceData and ImageResources tags
https://pypi.org/project/psdtags
BSD 3-Clause "New" or "Revised" License
19 stars 3 forks source link

TIFF layers to png convertion #8

Closed kmanimurugan closed 1 year ago

kmanimurugan commented 1 year ago

Can you please provide any example for TIFF layers to png convertion

cgohlke commented 1 year ago

Try following the example and write the layer data as PNG, e.g., using imagecodecs.imwrite(layer.name + f'{channel.channelid}.png', channel.data)

https://github.com/cgohlke/psdtags/blob/3f4112df1e295ad58062bb902b1a4a00052478c2/psdtags/psdtags.py#L160-L167

kmanimurugan commented 1 year ago
for layer in isd.layers:
    print(layer.name)
    tifffile.imshow(layer.asarray(), title=layer.name)
    # mask = layer.mask
    # print(mask)
    # imwrite('_test.jp2', layer.channel)
    # # Image.fromarray(layer.mask).save('%s.png' % layer.name)
    for channel in layer.channels:
        ch = channel.data  # a numpy array
        imagecodecs.imwrite(layer.name + f'{channel.channelid}.png', channel.data)

Tried the above and got error

Traceback (most recent call last):
  File "/Users/manimurugank/Projects/Python/plusImageDownload/pulseTifImage.py", line 45, in <module>
    imagecodecs.imwrite(layer.name + f'{channel.channelid}.png', channel.data)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/imagecodecs/imagecodecs.py", line 1516, in imwrite
    image: bytes = codec(data, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "imagecodecs/_png.pyx", line 156, in imagecodecs._png.png_encode
ValueError: invalid data shape, strides, or dtype
cgohlke commented 1 year ago

Your layer data are in a format that is not supported by PNG. Convert it before writing or use another image format.

kmanimurugan commented 1 year ago

isd = imread('img.tif') number_layers = len(isd.layers) for layer in isd.layers: print(layer.name) for channel in layer.channels: ch = channel.data # a numpy array imagecodecs.imwrite(ch, layer.name , codec="png", level=1)

Can you please provide your email i will give my TIFF file also

Then can you please help me to extract PNG files

kmanimurugan commented 1 year ago

@cgohlke Can you please share your email i will send my tiff file with you ?

cgohlke commented 1 year ago
from imagecodecs import imwrite
from psdtags import TiffImageSourceData

isd = TiffImageSourceData.fromtiff('layered.tif')
for layer in isd.layers:
    for channel in layer.channels:
        data = channel.data
        print(layer.name, channel.channelid, data.shape, data.dtype)
        if data.dtype.kind == 'f':
            data = (data * 255).round().astype('uint8')
        imwrite(f'{layer.name}{channel.channelid}.png', data)