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

Error opening jpeg file #7879

Closed
bigcat88 opened this issue Mar 14, 2024 · 6 comments · Fixed by #7884
Closed

Error opening jpeg file #7879

bigcat88 opened this issue Mar 14, 2024 · 6 comments · Fixed by #7884

Comments

@bigcat88
Copy link
Contributor

bigcat88 commented Mar 14, 2024

Maybe it is something wrong on my setup, but never seen that Pillow had problems with jpeg files.
Unfortunately currently has no free time to dive and debug/fix it myself.

I will just create an issue, feel free to close it, if after test there will be no problem found.

What are your OS, Python and Pillow versions?

  • OS: macOS 14
  • Python: 3.11.6
  • Pillow: 10.2 / 10.3.0.dev0
from PIL import Image, ImageSequence

if __name__ == "__main__":
    i = Image.open("bad.jpeg")
    for b in ImageSequence.Iterator(i):
        print(b.getexif())  # SyntaxError: not a TIFF file (header b'http://n' not valid)
from PIL import Image

if __name__ == "__main__":
    i = Image.open("bad.jpeg")
    i.show() # OSError: image file is truncated (664585 bytes not processed)

Problematic file(removed)

@radarhere
Copy link
Member

radarhere commented Mar 15, 2024

For i.show(), loading truncated images is enough to fix this.

from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

if __name__ == "__main__":
    i = Image.open("bad.jpeg")
    i.show()

Also, as far as Pillow is concerned at least, this isn't a JPEG image, it's an MPO.

@bigcat88
Copy link
Contributor Author

bigcat88 commented Mar 15, 2024

For i.show(), loading truncated images is enough to fix this.

I doubt the file is corrupted or truncated..
As I remember it is just and exported file from Apple "Photos" app

@radarhere
Copy link
Member

Looking at the EXIF matter, I take page 10 of the MPO specification to make that APP1 markers should contain EXIF data in MPO images. However, your MPO image does not. I've created #7884 to update Pillow to be flexible, allowing your code to output this instead -

{322: 512, 323: 512, 296: 2, 34665: 246, 271: 'Apple', 272: 'iPhone 13 Pro', 305: '16.2', 274: 1, 306: '2023:02:25 11:13:13', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone 13 Pro'}
{322: 512, 323: 512, 296: 2, 34665: 246, 271: 'Apple', 272: 'iPhone 13 Pro', 305: '16.2', 274: 1, 306: '2023:02:25 11:13:13', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone 13 Pro'}
{322: 512, 323: 512, 296: 2, 34665: 246, 271: 'Apple', 272: 'iPhone 13 Pro', 305: '16.2', 274: 1, 306: '2023:02:25 11:13:13', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone 13 Pro'}
{322: 512, 323: 512, 296: 2, 34665: 246, 271: 'Apple', 272: 'iPhone 13 Pro', 305: '16.2', 274: 1, 306: '2023:02:25 11:13:13', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone 13 Pro'}
{322: 512, 323: 512, 296: 2, 34665: 246, 271: 'Apple', 272: 'iPhone 13 Pro', 305: '16.2', 274: 1, 306: '2023:02:25 11:13:13', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone 13 Pro'}
{322: 512, 323: 512, 296: 2, 34665: 246, 271: 'Apple', 272: 'iPhone 13 Pro', 305: '16.2', 274: 1, 306: '2023:02:25 11:13:13', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone 13 Pro'}
{322: 512, 323: 512, 296: 2, 34665: 246, 271: 'Apple', 272: 'iPhone 13 Pro', 305: '16.2', 274: 1, 306: '2023:02:25 11:13:13', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone 13 Pro'}
{322: 512, 323: 512, 296: 2, 34665: 246, 271: 'Apple', 272: 'iPhone 13 Pro', 305: '16.2', 274: 1, 306: '2023:02:25 11:13:13', 531: 1, 282: 72.0, 283: 72.0, 316: 'iPhone 13 Pro'}

@bigcat88
Copy link
Contributor Author

Thank you, it was mainly the Exif reading code that worried me. Issue can be closed)

@radarhere
Copy link
Member

Actually, I've updated the PR to fix the reading of the size from your image as well, so the truncation problem is also fixed.

@bigcat88
Copy link
Contributor Author

Wow, this is amazing news, it's nice to know that even photos that do not fully comply with the JPEG format will no longer cause exceptions :)

Thank you again, good job.

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

Successfully merging a pull request may close this issue.

2 participants