1
1
from unittest import mock
2
2
3
+ import pyheif
3
4
import pytest
4
5
from PIL import Image
5
6
from pyheif import open as pyheif_open
6
7
7
8
from HeifImagePlugin import Transformations
8
9
9
- from . import respath
10
+ from . import avg_diff , respath
10
11
11
12
12
- skip_if_no_transformations = pytest .mark .skipif (
13
+ skip_no_transformations = pytest .mark .skipif (
13
14
Transformations is None ,
14
15
reason = "pyheif doesn't support transformations" )
15
16
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
+
16
21
17
22
def open_with_custom_meta (path , * , exif_data = None , exif = None , crop = None , orientation = 0 ):
18
23
def my_pyheif_open (* args , ** kwargs ):
@@ -47,14 +52,14 @@ def test_no_orientation_and_no_exif():
47
52
assert 'exif' not in image .info
48
53
49
54
50
- @skip_if_no_transformations
55
+ @skip_no_transformations
51
56
def test_empty_exif ():
52
57
image = open_with_custom_meta (respath ('test2.heic' ), exif_data = b'' , orientation = 1 )
53
58
assert 'exif' in image .info
54
59
assert image .getexif ()[274 ] == 1
55
60
56
61
57
- @skip_if_no_transformations
62
+ @skip_no_transformations
58
63
def test_broken_exif ():
59
64
broken = b'Exif\x00 \x00 II*\x00 \x02 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
60
65
image = open_with_custom_meta (respath ('test2.heic' ),
@@ -63,7 +68,7 @@ def test_broken_exif():
63
68
assert image .getexif ()[274 ] == 1
64
69
65
70
66
- @skip_if_no_transformations
71
+ @skip_no_transformations
67
72
def test_orientation_and_no_exif ():
68
73
image = open_with_custom_meta (respath ('test2.heic' ), orientation = 7 )
69
74
@@ -79,7 +84,7 @@ def test_no_orientation_and_exif_with_rotation():
79
84
assert image .getexif ()[274 ] == 7
80
85
81
86
82
- @skip_if_no_transformations
87
+ @skip_no_transformations
83
88
def test_orientation_and_exif_with_rotation ():
84
89
# Orientation tag from file should suppress Exif value
85
90
image = open_with_custom_meta (
@@ -89,7 +94,7 @@ def test_orientation_and_exif_with_rotation():
89
94
assert image .getexif ()[274 ] == 1
90
95
91
96
92
- @skip_if_no_transformations
97
+ @skip_no_transformations
93
98
def test_orientation_and_exif_without_rotation ():
94
99
image = open_with_custom_meta (
95
100
respath ('test2.heic' ), orientation = 1 , exif = {270 : "Sample image" })
@@ -98,7 +103,7 @@ def test_orientation_and_exif_without_rotation():
98
103
assert image .getexif ()[274 ] == 1
99
104
100
105
101
- @skip_if_no_transformations
106
+ @skip_no_transformations
102
107
def test_crop_on_load ():
103
108
ref_image = Image .open (respath ('test2.heic' ))
104
109
assert ref_image .size == (1280 , 720 )
@@ -110,3 +115,14 @@ def test_crop_on_load():
110
115
image = open_with_custom_meta (respath ('test2.heic' ), crop = (99 , 33 , 512 , 256 ))
111
116
assert image .size == (512 , 256 )
112
117
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