How to read a big tif file in python?

こ雲淡風輕ζ 提交于 2019-12-20 15:26:06

问题


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?


回答1:


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/




回答2:


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())



回答3:


I had huge tif files between 1 and 3 GB and managed to finally open them with Image.open() after manually changing the value of MAX_IMAGE_PIXELS inside the Image.py source code to an arbitrarily large number:

from PIL import Image
im = np.asarray(Image.open("location/image.tif")


来源:https://stackoverflow.com/questions/30465635/how-to-read-a-big-tif-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!