Skip to content

Commit f9a8dd0

Browse files
committed
Fix: Saving images in CMYK mode as RGBA
1 parent a4a1844 commit f9a8dd0

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ All notable changes to this project will be documented in this file.
2525

2626
- (Heif) `convert_to` should do nothing if the target `mode` is already the current image mode.
2727
- (AvifImagePlugin) do not register or accept `.avifs` files, libheif does not support them.
28+
- Images in `CMYK` mode will be converted for `RGBA` mode during saving instead of throwing `KeyError` exception.
2829

2930
## [0.7.0 - 2022-09-11]
3031

docs/image-modes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ When saving image from `Pillow` to `HEIF` format, next modes will be converted a
4545
* ``P`` will be converted to ``RGB``
4646
* ``I`` will be converted to ``I;16L``
4747
* ``1`` will be converted to ``L``
48+
* ``CMYK`` will be converted to ``RGBA``
4849

4950
.. _convert_to:
5051

pillow_heif/heif.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ def __add_frame_from_pillow(self, frame: Image.Image, ignore_primary: bool, **kw
617617
frame = frame.convert(mode="I;16L")
618618
elif frame.mode == "1":
619619
frame = frame.convert(mode="L")
620+
elif frame.mode == "CMYK":
621+
frame = frame.convert(mode="RGBA")
620622

621623
if original_orientation is not None and original_orientation != 1:
622624
frame = ImageOps.exif_transpose(frame)

tests/write_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,24 @@ def test_LA_color_mode(save_format): # noqa
264264
def test_1_color_mode():
265265
im = Image.linear_gradient(mode="L")
266266
im = im.convert(mode="1")
267+
assert im.mode == "1"
267268
out_heif = BytesIO()
268269
im.save(out_heif, format="HEIF", quality=-1)
269270
im_heif = Image.open(out_heif)
270271
assert im_heif.mode == "RGB"
271272
helpers.compare_hashes([im, im_heif], hash_size=8)
272273

273274

275+
def test_CMYK_color_mode(): # noqa
276+
im = helpers.gradient_rgba().convert("CMYK")
277+
assert im.mode == "CMYK"
278+
out_heif = BytesIO()
279+
im.save(out_heif, format="HEIF", quality=-1)
280+
im_heif = Image.open(out_heif)
281+
assert im_heif.mode == "RGBA"
282+
helpers.compare_hashes([im, im_heif], hash_size=8)
283+
284+
274285
@pytest.mark.parametrize("enc_bits", (10, 12))
275286
@pytest.mark.parametrize("save_format", ("HEIF", "AVIF"))
276287
def test_I_color_modes_to_10_12_bit(enc_bits, save_format): # noqa

0 commit comments

Comments
 (0)