Closed kmanimurugan closed 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)
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
Your layer data are in a format that is not supported by PNG. Convert it before writing or use another image format.
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
@cgohlke Can you please share your email i will send my tiff file with you ?
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)
Can you please provide any example for TIFF layers to png convertion