Skip to content

Commit

Permalink
Merge pull request python-pillow#7319 from radarhere/iptc
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Oct 5, 2023
2 parents 5f04b3d + dcfce94 commit 0c55a2d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
32 changes: 31 additions & 1 deletion Tests/test_file_iptc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from io import StringIO
from io import BytesIO, StringIO

from PIL import Image, IptcImagePlugin

Expand Down Expand Up @@ -30,6 +30,36 @@ def test_getiptcinfo_jpg_found():
assert iptc[(2, 101)] == b"Hungary"


def test_getiptcinfo_fotostation():
# Arrange
with open(TEST_FILE, "rb") as fp:
data = bytearray(fp.read())
data[86] = 240
f = BytesIO(data)
with Image.open(f) as im:
# Act
iptc = IptcImagePlugin.getiptcinfo(im)

# Assert
for tag in iptc.keys():
if tag[0] == 240:
return
assert False, "FotoStation tag not found"


def test_getiptcinfo_zero_padding():
# Arrange
with Image.open(TEST_FILE) as im:
im.info["photoshop"][0x0404] += b"\x00\x00\x00"

# Act
iptc = IptcImagePlugin.getiptcinfo(im)

# Assert
assert isinstance(iptc, dict)
assert len(iptc) == 3


def test_getiptcinfo_tiff_none():
# Arrange
with Image.open("Tests/images/hopper.tif") as im:
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/IptcImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ def field(self):
#
# get a IPTC field header
s = self.fp.read(5)
if not len(s):
if not s.strip(b"\x00"):
return None, 0

tag = s[1], s[2]

# syntax
if s[0] != 0x1C or tag[0] < 1 or tag[0] > 9:
if s[0] != 0x1C or tag[0] not in [1, 2, 3, 4, 5, 6, 7, 8, 9, 240]:
msg = "invalid IPTC/NAA file"
raise SyntaxError(msg)

Expand Down

0 comments on commit 0c55a2d

Please sign in to comment.