Skip to content

Commit

Permalink
Rotate images with EXIF rotation
Browse files Browse the repository at this point in the history
Fix #1667.
  • Loading branch information
liZe committed Jul 2, 2022
1 parent f2fed78 commit 2484ca2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions tests/draw/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,18 @@ def test_images_border_absolute(assert_pixels):
img { margin: 1px; border: 1px solid lime; position: absolute }
</style>
<div><img src="pattern.png"></div>''')


@assert_no_logs
def test_image_exif(assert_same_renderings):
assert_same_renderings(
'''
<style>@page { size: 10px }</style>
<img style="display: block" src="not-optimized.jpg">
''',
'''
<style>@page { size: 10px }</style>
<img style="display: block" src="not-optimized-exif.jpg">
''',
tolerance=25,
)
Binary file added tests/resources/not-optimized-exif.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion weasyprint/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from math import inf
from xml.etree import ElementTree

from PIL import Image
from PIL import Image, ImageOps

from .layout.percent import percentage
from .logger import LOGGER
Expand Down Expand Up @@ -131,6 +131,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()
if 'exif' in pillow_image.info:
pillow_image = ImageOps.exif_transpose(pillow_image)
image = RasterImage(pillow_image, image_id, optimize_size)

except (URLFetchingError, ImageLoadingError) as exception:
Expand Down

0 comments on commit 2484ca2

Please sign in to comment.