Skip to content

Commit

Permalink
Synonym for chroma encoder parameter: subsampling (#161)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 authored Nov 4, 2023
1 parent dc80c1a commit 5ac33e1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

### Added

- Synonym for `chroma` encoder parameter: `subsampling`(usage is the same as in Pillow JPEG). #161
- Pi-Heif: Python3.12 32-bit `armv7` wheels. #160

### Changed
Expand Down
8 changes: 8 additions & 0 deletions pillow_heif/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ def __init__(self, compression_format: HeifCompressionFormat, **kwargs):
self.ctx_write = _pillow_heif.CtxWrite(compression_format, -2 if quality is None else quality)
enc_params = kwargs.get("enc_params", {})
chroma = kwargs.get("chroma", None)
subsampling = kwargs.get("subsampling", None)
if chroma is None and subsampling:
subsampling_map = {
"4:4:4": 444,
"4:2:2": 422,
"4:2:0": 420,
}
chroma = subsampling_map.get(subsampling, None)
if chroma:
enc_params["chroma"] = chroma
for key, value in enc_params.items():
Expand Down
8 changes: 8 additions & 0 deletions tests/write_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ def test_chroma_heif_encoding_8bit(chroma, diff_epsilon, im):
im_out = Image.open(im_buf)
im = im.convert(mode=im_out.mode)
helpers.assert_image_similar(im, im_out, diff_epsilon)
im_buf_subsampling = BytesIO()
subsampling_map = {
444: "4:4:4",
422: "4:2:2",
420: "4:2:0",
}
im.save(im_buf_subsampling, format="HEIF", quality=-1, subsampling=subsampling_map[chroma])
assert im_buf.getbuffer().nbytes == im_buf_subsampling.getbuffer().nbytes # results should be the same


@pytest.mark.parametrize("chroma, diff_epsilon", ((420, 1.83), (422, 1.32), (444, 0.99)))
Expand Down

0 comments on commit 5ac33e1

Please sign in to comment.