Skip to content

Commit 828ff0b

Browse files
committed
fix for #13
1 parent 8c0a842 commit 828ff0b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pillow_heif/as_opener.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def _open(self):
2020
heif_file = open_heif(self.fp)
2121
except HeifError as e:
2222
raise SyntaxError(str(e)) from None
23-
if getattr(self, "_exclusive_fp", False):
24-
if hasattr(self, "fp") and self.fp is not None:
23+
if hasattr(self, "_exclusive_fp") and self._exclusive_fp:
24+
if hasattr(self, "fp") and self.fp:
2525
self.fp.close()
2626
self.fp = None
2727
self._size = heif_file.size
@@ -41,6 +41,9 @@ def _open(self):
4141
self.tile = []
4242
self.heif_file = heif_file
4343

44+
def verify(self) -> None:
45+
pass # we already check this in `_open`, no need to check second time.
46+
4447
def load(self):
4548
if self.heif_file is not None and self.heif_file:
4649
heif_file = self.heif_file.load()
@@ -58,3 +61,5 @@ def check_heif_magic(data) -> bool:
5861
def register_heif_opener():
5962
Image.register_open(HeifImageFile.format, HeifImageFile, check_heif_magic)
6063
Image.register_mime(HeifImageFile.format, "image/heif")
64+
Image.register_mime(HeifImageFile.format, "image/avif")
65+
Image.register_extensions(HeifImageFile.format, [".heic", ".heif", ".avif"])

tests/test_opener.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@
2020
@pytest.mark.parametrize("path", heic_files[:6] + hif_files[:6] + avif_files[:6])
2121
def test_open_image(path):
2222
image = Image.open(path)
23-
image.load()
2423
assert image is not None
2524

2625

26+
@pytest.mark.parametrize("path", heic_files[:2] + hif_files[:2] + avif_files[:2])
27+
def test_verify(path):
28+
image = Image.open(path)
29+
image.verify()
30+
assert image is not None
31+
assert not getattr(image, "fp", None)
32+
image.load()
33+
34+
2735
@pytest.mark.parametrize("path", heif_exif_test)
2836
def test_open_image_exif(path):
2937
image = Image.open(path)

0 commit comments

Comments
 (0)