webapp
webapp

Reputation: 708

How to convert a very large image(tif) file into an array using Python

I have a tif file of size ~2GB. I want to convert it into a numpy array for further processing. I tried to open the image using PIL.Image.open("FileName") and then add it to numpy array. But I am getting the error:

IOError: cannot identify image file

The file format is correct and also the location is accurately specified. Can you provide some information on why it might be happening? Do you think it has to do with the file size?

Upvotes: 1

Views: 2050

Answers (2)

jcupitt
jcupitt

Reputation: 11190

vips has good support for large files, and a convenient high-level Python binding, you could try that.

You can load images to memory like this:

$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvips
>>> im = pyvips.Image.new_from_file("huge.tif")
>>> im.width
29566
>>> im.height
14321
>>> y = im.write_to_memory()
>>> type(y)
<class '_cffi_backend.buffer'>
>>> len(y)
1270244058

And then make a numpy array from that object in the usual way. There's a chapter in the docs going into more detail on how to pass images back and forth between numpy, PIL and libvips.

What kind of further processing are you planning? You might be able to do what you need just using vips. It'd be a lot quicker.

Upvotes: 3

Anup Mishra
Anup Mishra

Reputation: 1

You can try Scipy:

from scipy import misc
f = misc.face()
misc.imsave('face.png', f) # uses the Image module (PIL)

import matplotlib.pyplot as plt
plt.imshow(f)
plt.show()

--Source: http://www.scipy-lectures.org/advanced/image_processing/

Upvotes: 0

Related Questions