I need a python routine that can open and import TIFF images into numpy arrays, so I can analyze and modify the contained data and afterwards save them as TIFFs again. (They are basically light intensity maps in greyscale, representing the respective values per pixel)
I tried to find something, but there is no documentation on PIL methods concerning TIFF. I tried to figure it out, but only got bad mode/ file type not supported errors.
What do I need to use here?
First, I downloaded a test TIFF image from this page called a_image.tif
. Then I opened with PIL like this:
>>> from PIL import Image >>> im = Image.open('a_image.tif') >>> im.show()
This showed the rainbow image. To convert to a numpy array, it's as simple as:
>>> import numpy >>> imarray = numpy.array(im)
We can see that the size of the image and the shape of the array match up:
>>> imarray.shape (44, 330) >>> im.size (330, 44)
And the array contains uint8
values:
>>> imarray array([[ 0, 1, 2, ..., 244, 245, 246], [ 0, 1, 2, ..., 244, 245, 246], [ 0, 1, 2, ..., 244, 245, 246], ..., [ 0, 1, 2, ..., 244, 245, 246], [ 0, 1, 2, ..., 244, 245, 246], [ 0, 1, 2, ..., 244, 245, 246]], dtype=uint8)
Once you're done modifying the array, you can turn it back into a PIL image like this:
>>> Image.fromarray(imarray)
I use matplotlib for reading TIFF files:
import matplotlib.pyplot as plt I = plt.imread(tiff_file)
and I
will be of type ndarray
.
According to the documentation though it is actually PIL that works behind the scenes when handling TIFFs as matplotlib only reads PNGs natively, but this has been working fine for me.
There's also a plt.imsave
function for saving.
You could also use GDAL to do this. I realize that it is a geospatial toolkit, but nothing requires you to have a cartographic product.
Link to precompiled GDAL binaries for windows (assuming windows here) http://www.gisinternals.com/sdk/
To access the array:
from osgeo import gdal dataset = gdal.Open("path/to/dataset.tiff", gdal.GA_ReadOnly) for x in range(1, dataset.RasterCount + 1): band = dataset.GetRasterBand(x) array = band.ReadAsArray()
pylibtiff worked better for me than PIL (which doesn't support color images with more than 8 bits per color).
from libtiff import TIFF tif = TIFF.open('filename.tif', mode='r') # read an image in the currect TIFF directory as a numpy array image = tif.read_image() # read all images in a TIFF file: for image in tif.iter_images(): pass tif = TIFF.open('filename.tif', mode='w') tif.write_image(image)
If you're on python3 you can't pip3 install libtiff
. Instead, manually install with
git clone git@github.com:pearu/pylibtiff.git python3 setup.py install
the readme of pylibtiff also mentions tifffile.py, but I haven't tried it.
You can also use pytiff of which I'm the author.
import pytiff with pytiff.Tiff("filename.tif") as handle: part = handle[100:200, 200:400] # multipage tif with pytiff.Tiff("multipage.tif") as handle: for page in handle: part = page[100:200, 200:400]
It's a fairly small module and may not have as many features as other modules, but it supports tiled tiffs and bigtiff, so you can read parts of large images.