cgohlke / imagecodecs

Image transformation, compression, and decompression codecs
https://pypi.org/project/imagecodecs
BSD 3-Clause "New" or "Revised" License
121 stars 24 forks source link

JxlEncoderProcessOutput returned JXL_ENC_ERROR when handling some images #71

Closed zhou13 closed 1 year ago

zhou13 commented 1 year ago

To reproduce the error, use:

import numpy as np
from imagecodecs import imwrite

buffer = np.load("1.npy")
print(buffer.shape, buffer.dtype)
imwrite("1.jxl", buffer)

Here is the example buffer: 1.npy.zip

I got error

(1536, 1536) float32
Traceback (most recent call last):
  File "reproduce.py", line 6, in <module>
    imwrite("1.jxl", buffer)
  File "/miniconda/envs/main/lib/python3.8/site-packages/imagecodecs/imagecodecs.py", line 1399, in imwrite
    image: bytes = codec(data, **kwargs)
  File "imagecodecs\_jpegxl.pyx", line 561, in imagecodecs._jpegxl.jpegxl_encode
imagecodecs._jpegxl.JpegxlError: JxlEncoderProcessOutput returned JXL_ENC_ERROR

The error seems to be size related rather than content-related: If I slice the buffer to be smaller, or if I do imwrite("1.jxl", np.float32(buffer.astype(np.float16))), the error will go away. The region I slice does not matter too much.

It would be helpful to print the error from JxlEncoderGetError so we can know what the error is.

cgohlke commented 1 year ago

Looks like a bug/limitation in libjxl. Try other encoding parameters (e.g. lossy mode with level=99) or use your workaround until the issue is fixed in a future version of libjxl.

It would be helpful to print the error from JxlEncoderGetError so we can know what the error is.

That's on the TODO list but would not help in this case as JxlEncoderGetError just returns JXL_ENC_ERR_GENERIC.