Skip to content

Commit 1a79d10

Browse files
authored
Merge pull request #8592 from radarhere/jpeg2000_cmyk_save
Support saving CMYK JP2 images
2 parents 41a89ea + 9bebecf commit 1a79d10

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

Tests/test_file_jpeg2k.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ def test_cmyk() -> None:
325325
assert im.getpixel((0, 0)) == (185, 134, 0, 0)
326326

327327

328+
@pytest.mark.skipif(
329+
not os.path.exists(EXTRA_DIR), reason="Extra image files not installed"
330+
)
331+
@skip_unless_feature_version("jpg_2000", "2.5.3")
332+
def test_cmyk_save() -> None:
333+
with Image.open(f"{EXTRA_DIR}/issue205.jp2") as jp2:
334+
assert jp2.mode == "CMYK"
335+
336+
im = roundtrip(jp2)
337+
assert_image_equal(im, jp2)
338+
339+
328340
@pytest.mark.parametrize("ext", (".j2k", ".jp2"))
329341
def test_16bit_monochrome_has_correct_mode(ext: str) -> None:
330342
with Image.open("Tests/images/16bit.cropped" + ext) as im:

docs/handbook/image-file-formats.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,19 @@ JPEG 2000
572572
Pillow reads and writes JPEG 2000 files containing ``L``, ``LA``, ``RGB``,
573573
``RGBA``, or ``YCbCr`` data. When reading, ``YCbCr`` data is converted to
574574
``RGB`` or ``RGBA`` depending on whether or not there is an alpha channel.
575-
Beginning with version 8.3.0, Pillow can read (but not write) ``RGB``,
576-
``RGBA``, and ``YCbCr`` images with subsampled components. Pillow supports
577-
JPEG 2000 raw codestreams (``.j2k`` files), as well as boxed JPEG 2000 files
578-
(``.jp2`` or ``.jpx`` files).
575+
576+
.. versionadded:: 8.3.0
577+
Pillow can read (but not write) ``RGB``, ``RGBA``, and ``YCbCr`` images with
578+
subsampled components.
579+
580+
.. versionadded:: 10.4.0
581+
Pillow can read ``CMYK`` images with OpenJPEG 2.5.1 and later.
582+
583+
.. versionadded:: 11.1.0
584+
Pillow can write ``CMYK`` images with OpenJPEG 2.5.3 and later.
585+
586+
Pillow supports JPEG 2000 raw codestreams (``.j2k`` files), as well as boxed
587+
JPEG 2000 files (``.jp2`` or ``.jpx`` files).
579588

580589
When loading, if you set the ``mode`` on the image prior to the
581590
:py:meth:`~PIL.Image.Image.load` method being invoked, you can ask Pillow to

docs/releasenotes/11.1.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ Reading JPEG 2000 comments
5858
When opening a JPEG 2000 image, the comment may now be read into
5959
:py:attr:`~PIL.Image.Image.info` for J2K images, not just JP2 images.
6060

61+
Saving JPEG 2000 CMYK images
62+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
64+
With OpenJPEG 2.5.3 or later, Pillow can now save CMYK images as JPEG 2000 files.
65+
6166
zlib-ng in wheels
6267
^^^^^^^^^^^^^^^^^
6368

src/libImaging/Jpeg2KEncode.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) {
330330
components = 4;
331331
color_space = OPJ_CLRSPC_SRGB;
332332
pack = j2k_pack_rgba;
333+
#if ((OPJ_VERSION_MAJOR == 2 && OPJ_VERSION_MINOR == 5 && OPJ_VERSION_BUILD >= 3) || \
334+
(OPJ_VERSION_MAJOR == 2 && OPJ_VERSION_MINOR > 5) || OPJ_VERSION_MAJOR > 2)
335+
} else if (strcmp(im->mode, "CMYK") == 0) {
336+
components = 4;
337+
color_space = OPJ_CLRSPC_CMYK;
338+
pack = j2k_pack_rgba;
339+
#endif
333340
} else {
334341
state->errcode = IMAGING_CODEC_BROKEN;
335342
state->state = J2K_STATE_FAILED;

0 commit comments

Comments
 (0)