Skip to content

Commit

Permalink
Merge pull request #7667 from nulano/iptc
Browse files Browse the repository at this point in the history
Fix loading IPTC images and update test
  • Loading branch information
radarhere committed Jan 1, 2024
2 parents d6fd4c9 + 3ef7b93 commit 4f17b60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
9 changes: 6 additions & 3 deletions Tests/test_file_iptc.py
Expand Up @@ -6,18 +6,21 @@

from PIL import Image, IptcImagePlugin

from .helper import hopper
from .helper import assert_image_equal, hopper

TEST_FILE = "Tests/images/iptc.jpg"


def test_open():
expected = Image.new("L", (1, 1))

f = BytesIO(
b"\x1c\x03<\x00\x02\x01\x00\x1c\x03x\x00\x01\x01\x1c"
b"\x03\x14\x00\x01\x01\x1c\x03\x1e\x00\x01\x01\x1c\x08\n\x00\x00"
b"\x1c\x03<\x00\x02\x01\x00\x1c\x03x\x00\x01\x01\x1c\x03\x14\x00\x01\x01"
b"\x1c\x03\x1e\x00\x01\x01\x1c\x08\n\x00\x01\x00"
)
with Image.open(f) as im:
assert im.tile == [("iptc", (0, 0, 1, 1), 25, "raw")]
assert_image_equal(im, expected)


def test_getiptcinfo_jpg_none():
Expand Down
25 changes: 7 additions & 18 deletions src/PIL/IptcImagePlugin.py
Expand Up @@ -16,8 +16,7 @@
#
from __future__ import annotations

import os
import tempfile
from io import BytesIO
from typing import Sequence

from . import Image, ImageFile
Expand Down Expand Up @@ -158,12 +157,11 @@ def load(self):
self.fp.seek(offset)

# Copy image data to temporary file
o_fd, outfile = tempfile.mkstemp(text=False)
o = os.fdopen(o_fd)
o = BytesIO()
if compression == "raw":
# To simplify access to the extracted file,
# 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:
type, size = self.field()
if type != (8, 10):
Expand All @@ -174,17 +172,10 @@ def load(self):
break
o.write(s)
size -= len(s)
o.close()

try:
with Image.open(outfile) as _im:
_im.load()
self.im = _im.im
finally:
try:
os.unlink(outfile)
except OSError:
pass
with Image.open(o) as _im:
_im.load()
self.im = _im.im


Image.register_open(IptcImageFile.format, IptcImageFile)
Expand All @@ -200,8 +191,6 @@ def getiptcinfo(im):
:returns: A dictionary containing IPTC information, or None if
no IPTC information block was found.
"""
import io

from . import JpegImagePlugin, TiffImagePlugin

data = None
Expand Down Expand Up @@ -236,7 +225,7 @@ class FakeImage:

# parse the IPTC information chunk
im.info = {}
im.fp = io.BytesIO(data)
im.fp = BytesIO(data)

try:
im._open()
Expand Down

0 comments on commit 4f17b60

Please sign in to comment.