5

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?

6
  • 1
    conventional conversion doesn't work - how doesn't it work? What is your output? How does it differ from what you expect? Do you receive an error? If so, what does the traceback say? Commented May 26, 2015 at 18:04
  • 2
    Try tifffile.py. Commented May 26, 2015 at 18:17
  • @That1Guy imarray is an empty array Commented May 26, 2015 at 21:26
  • Do you have a 64-bit Python? Commented May 26, 2015 at 22:15
  • @MarkRansom Yes it's 64-bits Commented May 27, 2015 at 0:41

5 Answers 5

4

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/

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

How do I install GDAL to use with python?
2

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

1 Comment

Ugh! Don't do that! Maintenance nightmare! Just change it in your own script with Image.MAX_IMAGE_PIXELS = None
2

you can try to use 'dask' library:

import dask_image.imread

ds = dask_image.imread.imread('name.tif')

Comments

1

For Python 32 bit, version 2.7 you are limited by the number of bytes you can add to the stack at a given time. One option is to read in the image in parts and then resize the individual chunks and reassemble them into a image that requires less RAM.

I recommend using the packages libtiff and opencv for that.

import os
os.environ["PATH"] += os.pathsep + "C:\\Program Files (x86)\\GnuWin32\\bin"
import numpy as np
import libtiff
import cv2

tif = libtiff.TIFF.open("HUGETIFFILE.tif", 'r')
width = tif.GetField("ImageWidth")
height = tif.GetField("ImageLength")
bits = tif.GetField('BitsPerSample')
sample_format = tif.GetField('SampleFormat')

ResizeFactor = 10 #Reduce Image Size by 10
Chunks = 8 #Read Image in 8 Chunks to prevent Memory Error (can be increased for 
# bigger files)

ReadStrip = tif.ReadEncodedStrip
typ = tif.get_numpy_type(bits, sample_format)


#ReadStrip
newarr = np.zeros((1, width/ResizeFactor), typ)
for ii in range(0,Chunks):
    pos = 0
    arr = np.empty((height/Chunks, width), typ)
    size = arr.nbytes
    for strip in range((ii*tif.NumberOfStrips()/Chunks),((ii+1)*tif.NumberOfStrips()/Chunks)):
        elem = ReadStrip(strip, arr.ctypes.data + pos, max(size-pos, 0))
        pos = pos + elem

    resized = cv2.resize(arr, (0,0), fx=float(1)/float(ResizeFactor), fy=float(1)/float(ResizeFactor))

    # Now remove the large array to free up Memory for the next chunk
    del arr
    # Finally recombine the individual resized chunks into the final resized image.
    newarr = np.vstack((newarr,resized))

newarr = np.delete(newarr, (0), axis=0)
cv2.imwrite('resized.tif', newarr)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.