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

Inconsistent behavior opening truncated JPEG file #2977

Closed
elbaro opened this issue Jan 25, 2018 · 5 comments
Closed

Inconsistent behavior opening truncated JPEG file #2977

elbaro opened this issue Jan 25, 2018 · 5 comments
Labels
Bug Any unexpected behavior, until confirmed feature. NumPy
Projects

Comments

@elbaro
Copy link

elbaro commented Jan 25, 2018

What did you do?

from PIL import Image
import numpy as np
a=Image.open('aa.jpg')
b1=np.array(a)
b2=np.array(a)

What did you expect to happen?

b1.shape == b2.shape == (H,W,3)

What actually happened?

Surprisingly, b1.shape == () and b2.shape == (H, W, 3).

b1:
array(<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=2000x2000 at 0x7FCD...48>, dtype=object)

I have a large RAM available (>50G).
This is consistently reproduced for a specific image file:
https://farm5.staticflickr.com/4360/36444958735_32c0410b64_o.jpg

What versions of Pillow and Python are you using?

5.1.0.dev0 && 4.3.0 (pip version)
Python 3.6.4

@elbaro
Copy link
Author

elbaro commented Jan 25, 2018

Many viewers can see it but viewnior failed to open it.
If there is something wrong with the image, I expect an explicit raise or consistent behavior.

@wiredfool
Copy link
Member

wiredfool commented Jan 25, 2018

We do throw an error on the first try, but apparently it's silently caught and discarded in numpy. Though, it is a bit of a puzzle as to why it succeeds the second time after the initial failure due to a truncated file.

>>> im = Image.open('36444958735_32c0410b64_o.jpg')
>>> im.__array_interface__.keys()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/erics/vpy35/lib/python3.5/site-packages/Pillow-5.0.0-py3.5-linux-x86_64.egg/PIL/Image.py", line 673, in __array_interface__
  File "/home/erics/vpy35/lib/python3.5/site-packages/Pillow-5.0.0-py3.5-linux-x86_64.egg/PIL/Image.py", line 720, in tobytes
  File "/home/erics/vpy35/lib/python3.5/site-packages/Pillow-5.0.0-py3.5-linux-x86_64.egg/PIL/ImageFile.py", line 233, in load
OSError: image file is truncated (13 bytes not processed)
>>> im.__array_interface__.keys()
dict_keys(['version', 'shape', 'data', 'typestr'])
>>> 

This is consistent behavior with two load calls, so it's something in the ImageFile code handling truncated files where it's not cleaning up the failure properly, and allowing the attempt to do it again.

@wiredfool wiredfool added the Bug Any unexpected behavior, until confirmed feature. label Feb 1, 2018
@wiredfool wiredfool changed the title Fail to convert to ndarray Inconsistent behavior opening truncated Jpeg file Feb 1, 2018
@wiredfool wiredfool changed the title Inconsistent behavior opening truncated Jpeg file Inconsistent behavior opening truncated JPEG file Feb 1, 2018
@aclark4life aclark4life added NumPy and removed NumPy labels Jun 30, 2018
@aclark4life aclark4life added this to Backlog in Pillow May 11, 2019
@aclark4life aclark4life moved this from Backlog to Icebox in Pillow May 11, 2019
@radarhere
Copy link
Member

With #3965, the behaviour is now consistent - the shape is empty for both calls.

@radarhere
Copy link
Member

You may also be interested to know that if you use LOAD_TRUNCATED_IMAGES, the dimensions are returned -

from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import numpy as np
a=Image.open('aa.jpg')
b1=np.array(a)
print(b1.shape)  # (2000, 2000, 3)
b2=np.array(a)
print(b2.shape)  # (2000, 2000, 3)

@radarhere
Copy link
Member

The fact that the errors are silenced is actually acknowledged as a problem with numpy - numpy/numpy#11629. So I'm going to close this.

Pillow automation moved this from Icebox to Closed Sep 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature. NumPy
Projects
Pillow
  
Closed
Development

No branches or pull requests

4 participants