Merge pull request #7667 from nulano/iptc

Fix loading IPTC images and update test
This commit is contained in:
Andrew Murray 2024-01-01 16:25:29 +11:00 committed by GitHub
commit 4f17b60088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 21 deletions

View File

@ -6,18 +6,21 @@ import pytest
from PIL import Image, IptcImagePlugin from PIL import Image, IptcImagePlugin
from .helper import hopper from .helper import assert_image_equal, hopper
TEST_FILE = "Tests/images/iptc.jpg" TEST_FILE = "Tests/images/iptc.jpg"
def test_open(): def test_open():
expected = Image.new("L", (1, 1))
f = BytesIO( f = BytesIO(
b"\x1c\x03<\x00\x02\x01\x00\x1c\x03x\x00\x01\x01\x1c" b"\x1c\x03<\x00\x02\x01\x00\x1c\x03x\x00\x01\x01\x1c\x03\x14\x00\x01\x01"
b"\x03\x14\x00\x01\x01\x1c\x03\x1e\x00\x01\x01\x1c\x08\n\x00\x00" b"\x1c\x03\x1e\x00\x01\x01\x1c\x08\n\x00\x01\x00"
) )
with Image.open(f) as im: with Image.open(f) as im:
assert im.tile == [("iptc", (0, 0, 1, 1), 25, "raw")] assert im.tile == [("iptc", (0, 0, 1, 1), 25, "raw")]
assert_image_equal(im, expected)
def test_getiptcinfo_jpg_none(): def test_getiptcinfo_jpg_none():

View File

@ -16,8 +16,7 @@
# #
from __future__ import annotations from __future__ import annotations
import os from io import BytesIO
import tempfile
from typing import Sequence from typing import Sequence
from . import Image, ImageFile from . import Image, ImageFile
@ -158,12 +157,11 @@ class IptcImageFile(ImageFile.ImageFile):
self.fp.seek(offset) self.fp.seek(offset)
# Copy image data to temporary file # Copy image data to temporary file
o_fd, outfile = tempfile.mkstemp(text=False) o = BytesIO()
o = os.fdopen(o_fd)
if compression == "raw": if compression == "raw":
# To simplify access to the extracted file, # To simplify access to the extracted file,
# prepend a PPM header # prepend a PPM header
o.write("P5\n%d %d\n255\n" % self.size) o.write(b"P5\n%d %d\n255\n" % self.size)
while True: while True:
type, size = self.field() type, size = self.field()
if type != (8, 10): if type != (8, 10):
@ -174,17 +172,10 @@ class IptcImageFile(ImageFile.ImageFile):
break break
o.write(s) o.write(s)
size -= len(s) size -= len(s)
o.close()
try: with Image.open(o) as _im:
with Image.open(outfile) as _im:
_im.load() _im.load()
self.im = _im.im self.im = _im.im
finally:
try:
os.unlink(outfile)
except OSError:
pass
Image.register_open(IptcImageFile.format, IptcImageFile) Image.register_open(IptcImageFile.format, IptcImageFile)
@ -200,8 +191,6 @@ def getiptcinfo(im):
:returns: A dictionary containing IPTC information, or None if :returns: A dictionary containing IPTC information, or None if
no IPTC information block was found. no IPTC information block was found.
""" """
import io
from . import JpegImagePlugin, TiffImagePlugin from . import JpegImagePlugin, TiffImagePlugin
data = None data = None
@ -236,7 +225,7 @@ def getiptcinfo(im):
# parse the IPTC information chunk # parse the IPTC information chunk
im.info = {} im.info = {}
im.fp = io.BytesIO(data) im.fp = BytesIO(data)
try: try:
im._open() im._open()