Skip to content

Commit

Permalink
Keep format when transposing images
Browse files Browse the repository at this point in the history
Fix #1755.
  • Loading branch information
liZe committed Oct 27, 2022
1 parent 66e1656 commit 632be52
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests/draw/test_image.py
Expand Up @@ -2,7 +2,8 @@

import pytest

from ..testing_utils import assert_no_logs, capture_logs
from ..testing_utils import (
FakeHTML, assert_no_logs, capture_logs, resource_filename)

centered_image = '''
________
Expand Down Expand Up @@ -578,3 +579,15 @@ def test_image_exif_image_orientation(assert_same_renderings):
''',
tolerance=25,
)


@assert_no_logs
def test_image_exif_image_orientation_keep_format():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/1755
pdf = FakeHTML(
string='''
<style>@page { size: 10px }</style>
<img style="display: block; image-orientation: 180deg"
src="not-optimized-exif.jpg">''',
base_url=resource_filename('<inline HTML>')).write_pdf()
assert b'DCTDecode' in pdf
3 changes: 3 additions & 0 deletions weasyprint/images.py
Expand Up @@ -135,6 +135,8 @@ def get_image_from_uri(cache, url_fetcher, optimize_size, url,
else:
# Store image id to enable cache in Stream.add_image
image_id = md5(url.encode()).hexdigest()
# Keep image format as it is discarded by transposition
image_format = pillow_image.format
if orientation == 'from-image':
if 'exif' in pillow_image.info:
pillow_image = ImageOps.exif_transpose(
Expand All @@ -148,6 +150,7 @@ def get_image_from_uri(cache, url_fetcher, optimize_size, url,
if flip:
pillow_image = pillow_image.transpose(
Image.Transpose.FLIP_LEFT_RIGHT)
pillow_image.format = image_format
image = RasterImage(pillow_image, image_id, optimize_size)

except (URLFetchingError, ImageLoadingError) as exception:
Expand Down

0 comments on commit 632be52

Please sign in to comment.