Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of image to array fails #3863

Closed
sommerfelt opened this issue May 23, 2019 · 5 comments · Fixed by #3965
Closed

Conversion of image to array fails #3863

sommerfelt opened this issue May 23, 2019 · 5 comments · Fixed by #3965
Projects

Comments

@sommerfelt
Copy link

sommerfelt commented May 23, 2019

Tried to load and convert an image (attached) to a numpy array. Fails on first try, succeeds on second. Strange!

Guessing cause may be that the image is slightly corrupted, but this is not detected or reported by PIL.

What are your OS, Python and Pillow versions?

  • OS: Windows 10 pro
  • Python: Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
  • Pillow: 6.0.0

doesNotConvertToArray

import numpy as np
from PIL import Image
# This image is to some extent corrupted, but is displayed correctly by viewing programs:
im = Image.open("doesNotConvertToArray.jpg") 
a1 = np.asarray(im) # First conversion fails
print(repr(a1))
# Only a singleton object array is printed: 
# array(<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=2703x1018 at 0x1AA4F6E1048>, dtype=object)

a2 = np.asarray(im) # Succeeds, strangely
print(a2.shape) # Prints correct shape: (1018, 2703, 3)
@radarhere
Copy link
Member

I find that if I add in ImageFile.LOAD_TRUNCATED_IMAGES = True, it works.

>>> import numpy as np
>>> from PIL import Image, ImageFile
>>> ImageFile.LOAD_TRUNCATED_IMAGES = True
>>> im = Image.open("doesNotConvertToArray.jpg") 
>>> a1 = np.asarray(im)
>>> print(a1.shape)
(1018, 2703, 3)
>>> a2 = np.asarray(im)
>>> print(a2.shape)
(1018, 2703, 3)

@hugovk
Copy link
Member

hugovk commented May 23, 2019

@sommerfelt Did that help? Okay to close this issue?

@sommerfelt
Copy link
Author

sommerfelt commented May 23, 2019

I reported the issue more because it indicates a bug in PIL and not so much because it causes a problem for me. In my case I will use a1.dtype==object to flag a corrupted image rather than cloaking it by setting ImageFile.LOAD_TRUNCATED_IMAGES = True.

So to conclude: For my purposes the issue is closed if I can rely on this behaviour, but for PIL it remains either a bug (an Exception should be raised unless ImageFile.LOAD_TRUNCATED_IMAGES = True) or if you like, it could be called an undocumented feature.

@radarhere radarhere added this to Backlog in Pillow May 25, 2019
@radarhere radarhere moved this from Backlog to In progress in Pillow May 25, 2019
@radarhere
Copy link
Member

I've created PR #3965 to resolve this, by fixing the inconsistent behaviour.

@youssouf-abd
Copy link

youssouf-abd commented Sep 11, 2020

I had the same problem.
When I opened my images in the grayscale mode, the problem did not appear. I am not expert, I don't know why it worked. But at least this comment may help other people.

To do so:

im = Image.open("doesNotConvertToArray.jpg").convert('L')
a1 = np.asarray(im) 
a2 = np.asarray(im) 
print a1
print a2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Pillow
  
Closed
Development

Successfully merging a pull request may close this issue.

4 participants