Search code examples
pythontiff

How do I modify TIFF physical resolution metadata


I have several pyramidal, tiled TIFF images that were converted from a different format. The converter program wrote incorrect data to the XResolution and YResolution TIFF metadata. How can I modify these fields?

tiff.ResolutionUnit: 'centimeter'
tiff.XResolution: '0.34703996762331574'
tiff.YResolution: '0.34704136833246829'

Ideally I would like to use Python or a command-line tool.


Solution

  • One can use tifftools.tiff_set from Tiff Tools.

    import tifftools
    
    tifftools.tiff_set(
        PATH_TO_ORIG_IMAGE,
        PATH_TO_NEW_IMAGE,
        overwrite=False,
        setlist=[
            (
                tifftools.Tag.RESOLUTIONUNIT,
                tifftools.constants.ResolutionUnit.CENTIMETER.value,
            ),
            (tifftools.Tag.XRESOLUTION, xresolution),
            (tifftools.Tag.YRESOLUTION, yresolution),
        ],
    )
    

    Replace xresolution and yresolution with the desired values. These values must be floats. In this example, the resolution unit is centimeter.


    This is also possible with the excellent tifffile package. In fact there is an example of this use case in the README.

    with TiffFile('temp.tif', mode='r+') as tif:
        _ = tif.pages[0].tags['XResolution'].overwrite((96000, 1000))
    

    Be aware that this will overwrite the original image. If this is not desired, make a copy of the image first and then overwrite the tags.