Skip to content

Commit

Permalink
Fix: Saving images in CMYK mode as RGBA
Browse files Browse the repository at this point in the history
  • Loading branch information
bigcat88 committed Oct 26, 2022
1 parent a4a1844 commit f9a8dd0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All notable changes to this project will be documented in this file.

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

## [0.7.0 - 2022-09-11]

Expand Down
1 change: 1 addition & 0 deletions docs/image-modes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ When saving image from `Pillow` to `HEIF` format, next modes will be converted a
* ``P`` will be converted to ``RGB``
* ``I`` will be converted to ``I;16L``
* ``1`` will be converted to ``L``
* ``CMYK`` will be converted to ``RGBA``

.. _convert_to:

Expand Down
2 changes: 2 additions & 0 deletions pillow_heif/heif.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ def __add_frame_from_pillow(self, frame: Image.Image, ignore_primary: bool, **kw
frame = frame.convert(mode="I;16L")
elif frame.mode == "1":
frame = frame.convert(mode="L")
elif frame.mode == "CMYK":
frame = frame.convert(mode="RGBA")

if original_orientation is not None and original_orientation != 1:
frame = ImageOps.exif_transpose(frame)
Expand Down
11 changes: 11 additions & 0 deletions tests/write_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,24 @@ def test_LA_color_mode(save_format): # noqa
def test_1_color_mode():
im = Image.linear_gradient(mode="L")
im = im.convert(mode="1")
assert im.mode == "1"
out_heif = BytesIO()
im.save(out_heif, format="HEIF", quality=-1)
im_heif = Image.open(out_heif)
assert im_heif.mode == "RGB"
helpers.compare_hashes([im, im_heif], hash_size=8)


def test_CMYK_color_mode(): # noqa
im = helpers.gradient_rgba().convert("CMYK")
assert im.mode == "CMYK"
out_heif = BytesIO()
im.save(out_heif, format="HEIF", quality=-1)
im_heif = Image.open(out_heif)
assert im_heif.mode == "RGBA"
helpers.compare_hashes([im, im_heif], hash_size=8)


@pytest.mark.parametrize("enc_bits", (10, 12))
@pytest.mark.parametrize("save_format", ("HEIF", "AVIF"))
def test_I_color_modes_to_10_12_bit(enc_bits, save_format): # noqa
Expand Down

0 comments on commit f9a8dd0

Please sign in to comment.