Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a big tif file in python?

I'm loading a tiff file from http://oceancolor.gsfc.nasa.gov/DOCS/DistFromCoast/

from PIL import Image
im = Image.open('GMT_intermediate_coast_distance_01d.tif')

The data is large (im.size=(36000, 18000) 1.3GB) and conventional conversion doesn't work; i.e, imarray.shape returns ()

import numpy as np 
imarray=np.zeros(im.size)
imarray=np.array(im)

How can I convert this tiff file to a numpy.array?

like image 274
ilciavo Avatar asked May 26 '15 17:05

ilciavo


People also ask

How can I open a big TIF file?

Just install our software TIFF Viewer for Google Chrome™ To begin viewing your TIFF files, simply do the following Install the software TIFF Viewer for Google Chrome™ Click on the software icon Find the TIFF file you wish to open Its that simple, begin viewing your TIFF files online today!

Can OpenCV read TIFF?

So, OpenCV can always read JPEGs, PNGs, and TIFFs.


2 Answers

May you dont have too much Ram for this image.You'll need at least some more than 1.3GB free memory.

I don't know what you're doing with the image and you read the entire into your memory but i recommend you to read it bit by bit if its possible to avoid blowing up your computer. You can use Image.getdata() which returns one pixel per time.

Also read some more for Image.open on this link :

http://www.pythonware.com/library/pil/handbook/

like image 158
gtzinos Avatar answered Nov 14 '22 22:11

gtzinos


So far I have tested many alternatives but only gdal worked always even with huge 16bit images.

You can open an image with something like this:

from osgeo import gdal
import numpy as np
ds = gdal.Open("name.tif")
channel = np.array(ds.GetRasterBand(1).ReadAsArray())
like image 25
G M Avatar answered Nov 14 '22 22:11

G M