1

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?

1
  • Can you please supply the actual code and perhaps a pointer to the source file? It's hard to diagnose unless we can reproduce the error. Commented Aug 11, 2016 at 22:42

2 Answers 2

3

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.

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

1 Comment

Thanks for the info :) this helps!
0

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/

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.