Skip to content

Instantly share code, notes, and snippets.

@h-mayorquin
Created November 21, 2025 17:36
Show Gist options
  • Select an option

  • Save h-mayorquin/fc5b9c37f0f90e2699e8c3ad5b07f0cd to your computer and use it in GitHub Desktop.

Select an option

Save h-mayorquin/fc5b9c37f0f90e2699e8c3ad5b07f0cd to your computer and use it in GitHub Desktop.
Dimension order and array access of scan image files with tifffile

Tifffile library handling of axis names and array access

I am writing this gist to leave a script that show how tifffile interprets the tags of tiff of image width and image height and how that translates to the array access pattern.

TIFF Tag Mapping

According to the TIFF specification:

  • Tag 256 (ImageWidth): Number of columns (horizontal extent, X axis) = width
  • Tag 257 (ImageLength): Number of rows (vertical extent, Y axis) = height

Example: Non-Square ScanImage File

For the file scanimage_20220923_roi.tif (256×528 pixels):

import tifffile

with tifffile.TiffFile('scanimage_20220923_roi.tif') as tif:
    page = tif.pages[0]

    # TIFF tags show the standard convention
    print(f"ImageWidth (Tag 256):  {page.imagewidth}")   # 256 (columns, X, width)
    print(f"ImageLength (Tag 257): {page.imagelength}")  # 528 (rows, Y, height)

    # Array shape follows (height, width) convention
    print(f"Array shape: {page.shape}")  # (528, 256) = (height, width)
    print(f"Axes: {page.axes}")          # 'YX'

    # Load as NumPy array
    data = page.asarray()
    print(f"Data shape: {data.shape}")   # (528, 256) = (height, width)

    # Access pattern: data[row, column] = data[y, x] = data[height_idx, width_idx]
    pixel = data[264, 128]  # Row 264, Column 128

Output:

ImageWidth (Tag 256):  256
ImageLength (Tag 257): 528
Array shape: (528, 256)
Axes: 'YX'
Data shape: (528, 256)

Key Points

  1. TIFF standard: ImageWidth=256 (width), ImageLength=528 (height)
  2. Array shape: (528, 256) = (height, width) - height comes first
  3. Indexing: data[y, x] or data[row, column] where first index is height dimension
  4. Memory layout: C-order (row-major) makes width the fast-varying dimension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment