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

Crash when loading JP2 image depending on openjpeg version #8784

Open
jranalli opened this issue Mar 1, 2025 · 3 comments · May be fixed by #8786
Open

Crash when loading JP2 image depending on openjpeg version #8784

jranalli opened this issue Mar 1, 2025 · 3 comments · May be fixed by #8786

Comments

@jranalli
Copy link

jranalli commented Mar 1, 2025

What did you do?

Trying to load and get data from a jp2 image
000137.zip

When working on my dataset, this issue occurred for ~50% of the images, but not for the remainder, so is intermittent.

This worked for me on a previously created python environment, but not a fresh one I created today (with nothing but conda install pillow). After some debugging on my end, I was able to determine that the error does NOT occur when downgrading to openjpeg version 2.4.0, but does occur with openjpeg version 2.5.2. The only difference in the PIL.report is the openjpeg version, and I'm able to repeatably break/fix the issue by upgrading/downgrading openjpeg.

I don't know if that makes in an openjpeg problem or a pillow problem, but I'm reporting here because this is the only imaging library I am familiar with, and thus the only place I know how to test and produce the error.

What did you expect to happen?

File loads and yields data

What actually happened?

Code crashes with the following error

  File "<python-input-1>", line 1, in <module>
    _ = Image.open('000137.jp2').getdata()
  File "D:\Code\Miniconda\envs\piltest\Lib\site-packages\PIL\Image.py", line 1456, in getdata
    self.load()
    ~~~~~~~~~^^
  File "D:\Code\Miniconda\envs\piltest\Lib\site-packages\PIL\Jpeg2KImagePlugin.py", line 351, in load
    return ImageFile.ImageFile.load(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "D:\Code\Miniconda\envs\piltest\Lib\site-packages\PIL\ImageFile.py", line 319, in load
    raise _get_oserror(err_code, encoder=False)
OSError: broken data stream when reading image file

What are your OS, Python and Pillow versions?

  • OS: Windows 11
  • Python: 3.13.2 (tested on various down to 3.8)
  • Pillow: 11.1
--------------------------------------------------------------------
Pillow 11.1.0
Python 3.13.2 | packaged by Anaconda, Inc. | (main, Feb  6 2025, 18:49:14) [MSC v.1929 64 bit (AMD64)]
--------------------------------------------------------------------
Python executable is D:\Code\Miniconda\envs\piltest\python.exe
System Python files loaded from D:\Code\Miniconda\envs\piltest
--------------------------------------------------------------------
Python Pillow modules loaded from D:\Code\Miniconda\envs\piltest\Lib\site-packages\PIL
Binary Pillow modules loaded from D:\Code\Miniconda\envs\piltest\Lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.1.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.3.2
--- JPEG support ok, compiled for 9.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.1
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------

Code for producing the error on that file:

from PIL import Image
_ = Image.open('000137.jp2').getdata()
@radarhere
Copy link
Member

In earlier versions, OpenJPEG identified the color space as OPJ_CLRSPC_UNSPECIFIED. Now, it is being identified as OPJ_CLRSPC_GRAY.

Pillow doesn't know how to read data from a gray image with 4 channels. I've created #8786 to read the data like a normal RGBA image.

I don't suppose you have an image that we could add to our test suite, and distribute under the Pillow license? Something considerably smaller than 10mb would be ideal.

@jranalli
Copy link
Author

jranalli commented Mar 2, 2025

I'm going to be really honest and say that I'm sorry but I didn't create the file and am not sure how to get you a smaller sample. I took a glance at whether there was a way to make an arbitrary image, but don't have a lot of experience with the JP2000 format to make sure it picks up the 4 layers.

The big image came from the NYC GIS Clearinghouse (specifically 2018, Queens Orthoimagery), and to the best of my knowledge is licensed under CC BY 4.0. So that sounds like it might be sufficiently open as a license, could you just crop it to get a smaller file?

I gave that a shot myself, but don't have a tool to write the image that is retaining the troublesome formatting issue. If desired, I could help identify one of the tiles that fails but has more detail than the open ocean.

@radarhere
Copy link
Member

Thanks.

While a natural image is preferable, I was able to add a test to the PR by manually modifying one of our existing images.

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