11from unittest import mock
22
3+ import pyheif
34import pytest
45from PIL import Image
56from pyheif import open as pyheif_open
67
78from HeifImagePlugin import Transformations
89
9- from . import respath
10+ from . import avg_diff , respath
1011
1112
12- skip_if_no_transformations = pytest .mark .skipif (
13+ skip_no_transformations = pytest .mark .skipif (
1314 Transformations is None ,
1415 reason = "pyheif doesn't support transformations" )
1516
17+ skip_libheif_not_16 = pytest .mark .skipif (
18+ pyheif .libheif_version () < '1.16.0' ,
19+ reason = "libheif < 1.16.0 can't decode odd sizes" )
20+
1621
1722def open_with_custom_meta (path , * , exif_data = None , exif = None , crop = None , orientation = 0 ):
1823 def my_pyheif_open (* args , ** kwargs ):
@@ -47,14 +52,14 @@ def test_no_orientation_and_no_exif():
4752 assert 'exif' not in image .info
4853
4954
50- @skip_if_no_transformations
55+ @skip_no_transformations
5156def test_empty_exif ():
5257 image = open_with_custom_meta (respath ('test2.heic' ), exif_data = b'' , orientation = 1 )
5358 assert 'exif' in image .info
5459 assert image .getexif ()[274 ] == 1
5560
5661
57- @skip_if_no_transformations
62+ @skip_no_transformations
5863def test_broken_exif ():
5964 broken = b'Exif\x00 \x00 II*\x00 \x02 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
6065 image = open_with_custom_meta (respath ('test2.heic' ),
@@ -63,7 +68,7 @@ def test_broken_exif():
6368 assert image .getexif ()[274 ] == 1
6469
6570
66- @skip_if_no_transformations
71+ @skip_no_transformations
6772def test_orientation_and_no_exif ():
6873 image = open_with_custom_meta (respath ('test2.heic' ), orientation = 7 )
6974
@@ -79,7 +84,7 @@ def test_no_orientation_and_exif_with_rotation():
7984 assert image .getexif ()[274 ] == 7
8085
8186
82- @skip_if_no_transformations
87+ @skip_no_transformations
8388def test_orientation_and_exif_with_rotation ():
8489 # Orientation tag from file should suppress Exif value
8590 image = open_with_custom_meta (
@@ -89,7 +94,7 @@ def test_orientation_and_exif_with_rotation():
8994 assert image .getexif ()[274 ] == 1
9095
9196
92- @skip_if_no_transformations
97+ @skip_no_transformations
9398def test_orientation_and_exif_without_rotation ():
9499 image = open_with_custom_meta (
95100 respath ('test2.heic' ), orientation = 1 , exif = {270 : "Sample image" })
@@ -98,7 +103,7 @@ def test_orientation_and_exif_without_rotation():
98103 assert image .getexif ()[274 ] == 1
99104
100105
101- @skip_if_no_transformations
106+ @skip_no_transformations
102107def test_crop_on_load ():
103108 ref_image = Image .open (respath ('test2.heic' ))
104109 assert ref_image .size == (1280 , 720 )
@@ -110,3 +115,14 @@ def test_crop_on_load():
110115 image = open_with_custom_meta (respath ('test2.heic' ), crop = (99 , 33 , 512 , 256 ))
111116 assert image .size == (512 , 256 )
112117 assert image .copy () == ref_image .crop ((99 , 33 , 611 , 289 ))
118+
119+
120+ @skip_libheif_not_16
121+ def test_fallback_to_transforms ():
122+ # Image with 695x472 color and 696x472 alpha with crop
123+ image = Image .open (respath ('unreadable-wo-transf.heic' ))
124+ assert image .size == (695 , 472 )
125+
126+ ref_image = Image .open (respath ('unreadable-wo-transf.ref.heic' ))
127+ avg_diffs = avg_diff (image , ref_image )
128+ assert max (avg_diffs ) <= 0.01
0 commit comments