Skip to content

Commit 632be52

Browse files
committed
Keep format when transposing images
Fix #1755.
1 parent 66e1656 commit 632be52

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

tests/draw/test_image.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import pytest
44

5-
from ..testing_utils import assert_no_logs, capture_logs
5+
from ..testing_utils import (
6+
FakeHTML, assert_no_logs, capture_logs, resource_filename)
67

78
centered_image = '''
89
________
@@ -578,3 +579,15 @@ def test_image_exif_image_orientation(assert_same_renderings):
578579
''',
579580
tolerance=25,
580581
)
582+
583+
584+
@assert_no_logs
585+
def test_image_exif_image_orientation_keep_format():
586+
# Regression test for https://github.com/Kozea/WeasyPrint/issues/1755
587+
pdf = FakeHTML(
588+
string='''
589+
<style>@page { size: 10px }</style>
590+
<img style="display: block; image-orientation: 180deg"
591+
src="not-optimized-exif.jpg">''',
592+
base_url=resource_filename('<inline HTML>')).write_pdf()
593+
assert b'DCTDecode' in pdf

weasyprint/images.py

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def get_image_from_uri(cache, url_fetcher, optimize_size, url,
135135
else:
136136
# Store image id to enable cache in Stream.add_image
137137
image_id = md5(url.encode()).hexdigest()
138+
# Keep image format as it is discarded by transposition
139+
image_format = pillow_image.format
138140
if orientation == 'from-image':
139141
if 'exif' in pillow_image.info:
140142
pillow_image = ImageOps.exif_transpose(
@@ -148,6 +150,7 @@ def get_image_from_uri(cache, url_fetcher, optimize_size, url,
148150
if flip:
149151
pillow_image = pillow_image.transpose(
150152
Image.Transpose.FLIP_LEFT_RIGHT)
153+
pillow_image.format = image_format
151154
image = RasterImage(pillow_image, image_id, optimize_size)
152155

153156
except (URLFetchingError, ImageLoadingError) as exception:

0 commit comments

Comments
 (0)