Skip to content

Instantly share code, notes, and snippets.

@ax3l
Last active January 10, 2024 16:49
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ax3l/5781ce80b19d7df3f549 to your computer and use it in GitHub Desktop.
Save ax3l/5781ce80b19d7df3f549 to your computer and use it in GitHub Desktop.
Python: Write tif

tifffile

(specific read/write library, no extras) https://github.com/blink1073/tifffile/blob/master/tifffile/tifffile.py

from tifffile import imsave
import numpy as np

# create data
d = np.ndarray(shape=(10,20), dtype=np.float32) # also supports 64bit but ImageJ does not
d[()] = np.arange(200).reshape(10, 20)

# save 32bit float (== single) tiff
imsave('test.tif', d) #, description="hohoho")

imsave: https://github.com/blink1073/tifffile/blob/0afb7799b12df6459fb7baa946db65f3ad0eceaa/tifffile/tifffile.py#L248-L271

Pillow

PIL clone (larger framework)

from PIL import Image
import numpy as np

# create data
d = np.ndarray(shape=(10,20), dtype=np.float32)
d[()] = np.arange(200).reshape(10, 20)

im = Image.fromarray(d, mode='F') # float32
im.save("test2.tiff", "TIFF")

https://pillow.readthedocs.org/en/3.0.0/handbook/image-file-formats.html#tiff

modes: https://pillow.readthedocs.org/en/3.0.x/handbook/concepts.html#modes

@neogeogre
Copy link

thx for the samples

@ax3l
Copy link
Author

ax3l commented Apr 25, 2020

Thank you for the kind words!

@muyuuuu
Copy link

muyuuuu commented Mar 7, 2022

thank you.

@rym-oualha
Copy link

Thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment