Skip to content

Instantly share code, notes, and snippets.

@ij96
Created May 10, 2022 09:21
Show Gist options
  • Save ij96/6d1f98b4eacb50e9e1d90e661be5e967 to your computer and use it in GitHub Desktop.
Save ij96/6d1f98b4eacb50e9e1d90e661be5e967 to your computer and use it in GitHub Desktop.
Get TIFF image shape quickly, by reading the tags rather than the image.
import tifffile
def get_tiff_shape(im_path):
"""
Get TIFF image shape quickly, by reading the tags rather than the image.
"""
with tifffile.TiffFile(im_path) as tif:
c = len(tif.pages)
h = w = None
for tag in tif.pages[0].tags.values():
if tag.name == 'ImageLength':
h = tag.value
elif tag.name == 'ImageWidth':
w = tag.value
return (c, h, w)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment