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

Empty JPEG2000 image after upgrading from 10.2.0 to 10.3.0 #7945

Closed
stefan6419846 opened this issue Apr 4, 2024 · 5 comments · Fixed by #7947
Closed

Empty JPEG2000 image after upgrading from 10.2.0 to 10.3.0 #7945

stefan6419846 opened this issue Apr 4, 2024 · 5 comments · Fixed by #7947
Labels

Comments

@stefan6419846
Copy link
Contributor

What did you do?

I want to load a JPEG2000 image from raw data into Pillow for further processing. After upgrading from 10.2.0 to 10.3.0, this broke for a specific image.

What did you expect to happen?

The image is being loaded as before.

What actually happened?

The image is not being loaded correctly and just empty.

What are your OS, Python and Pillow versions?

  • OS: OpenSUSE Leap 15.4, Ubuntu 22.04 (through GitHub Actions)
  • Python: 3.9.18, 3.12.2 (through GitHub Actions)
  • Pillow: 10.3.0
--------------------------------------------------------------------
Pillow 10.3.0
Python 3.9.18 (main, Sep 06 2023, 07:49:32) [GCC]
--------------------------------------------------------------------
Python executable is /home/stefan/temp/venv/bin/python3
Environment Python files loaded from /home/stefan/temp/venv
System Python files loaded from /usr
--------------------------------------------------------------------
Python Pillow modules loaded from /home/stefan/temp/venv/lib64/python3.9/site-packages/PIL
Binary Pillow modules loaded from /home/stefan/temp/venv/lib64/python3.9/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.3.0
*** TKINTER support not installed
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 3.0.2
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.11
--- LIBTIFF support ok, loaded 4.6.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.10, harfbuzz 8.4.0
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
from PIL import Image, ImageFile
from io import BytesIO

ImageFile.LOAD_TRUNCATED_IMAGES = True

with open("raw.txt", mode="rb") as fd:
    data = fd.read()
image = Image.open(BytesIO(data), formats=("JPEG2000",))
print(image)
image.load()
image.save(f"{Image.__version__}.png")

Corresponding raw data: raw.txt

With version 10.2.0:

10 2 0

With version 10.3.0:

10 3 0

Corresponding issue where this was discovered: py-pdf/pypdf#2568

@radarhere
Copy link
Member

Investigating, the change didn't occur in Pillow's code itself, but rather as a result of upgrading from OpenJPEG 2.5.0 to 2.5.2.

@radarhere
Copy link
Member

I've created #7947 to resolve this. With that change,

from PIL import Image, ImageFile
from io import BytesIO

ImageFile.LOAD_TRUNCATED_IMAGES = True

with open("raw.txt", mode="rb") as fd:
    data = fd.read()
image = Image.open(BytesIO(data), formats=("JPEG2000",))
image.save(f"{Image.__version__}.jpg")

creates
10 4 0 dev0

This isn't the same as 10.2.0, but I think you will agree it is correct when compared to the original image data.

@pubpub-zz
Copy link

pubpub-zz commented Apr 6, 2024

@radarhere
can you tell me if the transparency is respected?

@radarhere
Copy link
Member

When I look at it in macOS Preview, or use ImageMagick to convert it to PNG (albeit with the colours changing), I'm not seeing any evidence that the image from this issue has transparency.

But regarding the general question of transparency in CMYK JPEG2000 images, no, that is not currently supported.

@stefan6419846
Copy link
Contributor Author

Thanks for the PR. I have just built a corresponding wheel for your branch and tested it with the original downstream setup and it looks fine.

@pubpub-zz The transparency aspects for Pillow do indeed change a bit, but due to applying the transparency mask in pypdf afterwards, the resulting image is visually identical.

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

Successfully merging a pull request may close this issue.

3 participants