Steve Boege
Steve Boege

Reputation: 81

How can pixel size be set for tiff images saved using python so that the pixel height and width are set to particular values in ImageJ?

There are numerous means {PIL, opencv, tifffile, et cetera} for creating tiff image files from numpy arrays. How can pixel size be set for tiff images saved using python so that the pixel height and width have particular values in ImageJ?

When a tiff image is opened in ImageJ, Ctrl+Shift+P opens an image properties window such as: enter image description here

There are tiff tags that include XResolution, YResolution, ResolutionUnit, PixelXDimension, PixelYDimension, et cetera. Which, if any, of these can be used to set a property of the saved image so that ImageJ uses specified Pixel width and height? Which packages enable setting of such tags during the save process?

Upvotes: 0

Views: 1600

Answers (1)

Steve Boege
Steve Boege

Reputation: 81

Christoph Gohlke, creator of tifffile, kindly taught me two things:

  1. tifffile will set XResolution, YResolution and ResolutionUnit tags if a resolution tuple is passed to tifffile.imsave. Building on What is the best way to save image metadata alongside a tif? from Jenny Folkesson, I added a resolution tuple

    tifffile.imsave(
    my_tiff,
    float64_im.astype('float32'),
    ijmetadata=ijmetadata,
    description=description,
    extratags=extra_tags,
    resolution=(1/pixel_pitch_in_cm, 1/pixel_pitch_in_cm, 'CENTIMETER'))
    
  2. ImageJ doesn't readily open tiffs having 64-bit float data. The work-around is to cast the image to 'float32' before passing it to tifffile.imsave.

Upvotes: 1

Related Questions