mirror of https://github.com/wanadev/yoga.git
JPEG: ignore invalid values for the the orientation tag (closes #38)
This commit is contained in:
parent
ac370d1b84
commit
8cd77aee9a
|
@ -56,7 +56,7 @@ Changelog
|
|||
|
||||
* **[NEXT]** (changes on ``master`` that have not been released yet):
|
||||
|
||||
* *nothing yet*
|
||||
* JPEG: ignore invalid values for the the orientation tag (#38)
|
||||
|
||||
* **1.1.0:**
|
||||
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 67 KiB |
|
@ -35,5 +35,25 @@ class Test_open_jpeg(object):
|
|||
assert r2 < 5 and g2 > 250 and b2 < 5 # ~lime
|
||||
|
||||
# Check if the blue square is at bottom-right corner
|
||||
r3, g3, b3 = image.getpixel((255 - 8, 340 - 8))
|
||||
r3, g3, b3 = image.getpixel((255 - 8, 341 - 8))
|
||||
assert r3 < 5 and g3 < 5 and b3 > 250 # ~blue
|
||||
|
||||
def test_jpeg_orientation_invalid(self):
|
||||
with open("test/images/orientation/invalid.jpg", "rb") as image_file:
|
||||
image = jpeg.open_jpeg(image_file)
|
||||
|
||||
# Test image size
|
||||
assert image.width == 341
|
||||
assert image.height == 256
|
||||
|
||||
# Check if the red square is at top-right corner
|
||||
r1, g1, b1 = image.getpixel((341 - 8, 8))
|
||||
assert r1 > 250 and g1 < 5 and b1 < 5 # ~red
|
||||
|
||||
# Check if the green square is at bottom-right corner
|
||||
r2, g2, b2 = image.getpixel((341 - 8, 255 - 8))
|
||||
assert r2 < 5 and g2 > 250 and b2 < 5 # ~lime
|
||||
|
||||
# Check if the blue square is at bottom-left corner
|
||||
r3, g3, b3 = image.getpixel((8, 255 - 8))
|
||||
assert r3 < 5 and g3 < 5 and b3 > 250 # ~blue
|
||||
|
|
|
@ -3,6 +3,19 @@ import mozjpeg_lossless_optimization
|
|||
from PIL import Image
|
||||
|
||||
|
||||
EXIF_TAG_ORIENTATION = 0x0112
|
||||
ORIENTATION_OPERATIONS = {
|
||||
1: [],
|
||||
2: [Image.FLIP_LEFT_RIGHT],
|
||||
3: [Image.ROTATE_180],
|
||||
4: [Image.FLIP_TOP_BOTTOM],
|
||||
5: [Image.FLIP_LEFT_RIGHT, Image.ROTATE_90],
|
||||
6: [Image.ROTATE_270],
|
||||
7: [Image.FLIP_LEFT_RIGHT, Image.ROTATE_270],
|
||||
8: [Image.ROTATE_90],
|
||||
}
|
||||
|
||||
|
||||
def is_jpeg(file_bytes):
|
||||
"""Whether or not the given bytes represent a JPEG file.
|
||||
|
||||
|
@ -45,22 +58,13 @@ def open_jpeg(image_file):
|
|||
:param file-like image_file: the image file.
|
||||
:rtype: PIL.Image
|
||||
"""
|
||||
EXIF_TAG_ORIENTATION = 274
|
||||
ORIENTATION_OPERATIONS = {
|
||||
1: [],
|
||||
2: [Image.FLIP_LEFT_RIGHT],
|
||||
3: [Image.ROTATE_180],
|
||||
4: [Image.FLIP_TOP_BOTTOM],
|
||||
5: [Image.FLIP_LEFT_RIGHT, Image.ROTATE_90],
|
||||
6: [Image.ROTATE_270],
|
||||
7: [Image.FLIP_LEFT_RIGHT, Image.ROTATE_270],
|
||||
8: [Image.ROTATE_90],
|
||||
}
|
||||
|
||||
image = Image.open(image_file)
|
||||
exif = image.getexif()
|
||||
|
||||
if EXIF_TAG_ORIENTATION in exif:
|
||||
if (
|
||||
EXIF_TAG_ORIENTATION in exif
|
||||
and exif[EXIF_TAG_ORIENTATION] in ORIENTATION_OPERATIONS
|
||||
):
|
||||
orientation = exif[EXIF_TAG_ORIENTATION]
|
||||
for operation in ORIENTATION_OPERATIONS[orientation]:
|
||||
image = image.transpose(operation)
|
||||
|
|
Loading…
Reference in New Issue