DigitalSlideArchive/tifftools

Appending a new tag to an existing TIFF image

pfculverhouse opened this issue · 3 comments

Hi,

I am trying to add a new tag to a TIF file using Python3.

I have worked through your code and I am now able to understand the TIFF structure.

However, I need to add a a new Tag. My code is an attempt to adapt your example for a similar action from the command line:

info = tifftools.read_tiff(Fpath)
info[info, 'ifds'][0]['tags'][tifftools.Tag.ImageDescription.value] = {
'data': 'A dog digging.',
'datatype': tifftools.Datatype.ASCII
}

tifftools.write_tiff(Fpath, OutPath, info)

I am not the strongest Python programmer, but I am baffled why I cannot update the TIFF TAG dict structure. Can you guide me please?

regards

Phil

In what you just posted, the line info[info, 'ifds'][0]['tags'][tifftools.Tag.ImageDescription.value] = { isn't correct at the beginning. It should be info['ifds'][0]['tags'][tifftools.Tag.ImageDescription.value] = { (you have an extra info, near the beginning).

The write_tiff method takes (info, outpath) as its arguments (you have something else)

Hi,

Thanks for speedy reply. When I do that, I get

tifftools.write_tiff(info, '/Users/culverhouse/Downloads/Python-Exif/out3.tif')
Traceback (most recent call last):
File "", line 1, in
File "/Users/culverhouse/Library/Python/3.7/lib/python/site-packages/tifftools/tifftools.py", line 302, in write_tiff
ifdPtr = write_ifd(dest, bom, bigtiff, ifd, ifdPtr, ifdsFirst=ifdsFirst)
File "/Users/culverhouse/Library/Python/3.7/lib/python/site-packages/tifftools/tifftools.py", line 471, in write_ifd
write_sub_ifds(dest, bom, bigtiff, ifd, pos, subifdPtrs, ifdsFirst=ifdsFirst)
File "/Users/culverhouse/Library/Python/3.7/lib/python/site-packages/tifftools/tifftools.py", line 508, in write_sub_ifds
getattr(tag, 'tagset', None), ifdsFirst=ifdsFirst)
File "/Users/culverhouse/Library/Python/3.7/lib/python/site-packages/tifftools/tifftools.py", line 436, in write_ifd
data = data.encode() + b'\x00'
AttributeError: 'bytes' object has no attribute 'encode'

I've just checked the out3.tif and it has the correct extra TAG inserted, so thank you. But I do not know why I get the error above.

Phil

Aaah, it appears the image TIF file is corrupted, I have used another image and it works fine. Thanks for your help.

Phil