2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2019-09-26 15:12:28 +03:00
|
|
|
import sys
|
2023-08-22 03:13:41 +03:00
|
|
|
from io import BytesIO, StringIO
|
2019-09-26 15:12:28 +03:00
|
|
|
|
2023-11-12 23:30:28 +03:00
|
|
|
import pytest
|
|
|
|
|
2014-07-30 20:34:20 +04:00
|
|
|
from PIL import Image, IptcImagePlugin
|
|
|
|
|
2023-12-31 15:47:37 +03:00
|
|
|
from .helper import assert_image_equal, hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2014-07-30 20:34:20 +04:00
|
|
|
TEST_FILE = "Tests/images/iptc.jpg"
|
|
|
|
|
|
|
|
|
2023-12-31 14:43:08 +03:00
|
|
|
def test_open():
|
2023-12-31 16:49:48 +03:00
|
|
|
expected = Image.new("L", (1, 1))
|
2023-12-31 15:47:37 +03:00
|
|
|
|
2023-12-31 14:43:08 +03:00
|
|
|
f = BytesIO(
|
2023-12-31 15:47:37 +03:00
|
|
|
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"
|
2023-12-31 14:43:08 +03:00
|
|
|
)
|
|
|
|
with Image.open(f) as im:
|
|
|
|
assert im.tile == [("iptc", (0, 0, 1, 1), 25, "raw")]
|
2023-12-31 15:47:37 +03:00
|
|
|
assert_image_equal(im, expected)
|
2023-12-31 14:43:08 +03:00
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_getiptcinfo_jpg_none():
|
|
|
|
# Arrange
|
|
|
|
with hopper() as im:
|
|
|
|
# Act
|
|
|
|
iptc = IptcImagePlugin.getiptcinfo(im)
|
2014-07-30 20:34:20 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Assert
|
|
|
|
assert iptc is None
|
2014-07-30 20:34:20 +04:00
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_getiptcinfo_jpg_found():
|
|
|
|
# Arrange
|
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
# Act
|
|
|
|
iptc = IptcImagePlugin.getiptcinfo(im)
|
2017-03-01 12:20:18 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Assert
|
|
|
|
assert isinstance(iptc, dict)
|
|
|
|
assert iptc[(2, 90)] == b"Budapest"
|
|
|
|
assert iptc[(2, 101)] == b"Hungary"
|
2017-03-01 12:20:18 +03:00
|
|
|
|
|
|
|
|
2023-08-22 03:13:41 +03:00
|
|
|
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
|
2023-11-12 23:30:28 +03:00
|
|
|
pytest.fail("FotoStation tag not found")
|
2023-08-22 03:13:41 +03:00
|
|
|
|
|
|
|
|
2023-08-04 16:54:05 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_getiptcinfo_tiff_none():
|
|
|
|
# Arrange
|
|
|
|
with Image.open("Tests/images/hopper.tif") as im:
|
2014-07-30 20:34:20 +04:00
|
|
|
# Act
|
2020-01-27 14:46:52 +03:00
|
|
|
iptc = IptcImagePlugin.getiptcinfo(im)
|
2014-07-30 20:34:20 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Assert
|
|
|
|
assert iptc is None
|
2014-07-30 20:34:20 +04:00
|
|
|
|
2014-08-01 12:56:21 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_i():
|
|
|
|
# Arrange
|
|
|
|
c = b"a"
|
|
|
|
|
|
|
|
# Act
|
2023-12-30 02:02:34 +03:00
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
ret = IptcImagePlugin.i(c)
|
2020-01-27 14:46:52 +03:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert ret == 97
|
|
|
|
|
|
|
|
|
2023-12-30 02:02:34 +03:00
|
|
|
def test_dump(monkeypatch):
|
2020-01-27 14:46:52 +03:00
|
|
|
# Arrange
|
|
|
|
c = b"abc"
|
|
|
|
# Temporarily redirect stdout
|
2023-12-30 02:02:34 +03:00
|
|
|
mystdout = StringIO()
|
|
|
|
monkeypatch.setattr(sys, "stdout", mystdout)
|
2020-01-27 14:46:52 +03:00
|
|
|
|
|
|
|
# Act
|
2023-12-30 02:02:34 +03:00
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
IptcImagePlugin.dump(c)
|
2014-08-01 12:56:21 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Assert
|
|
|
|
assert mystdout.getvalue() == "61 62 63 \n"
|
2023-12-30 02:02:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_pad_deprecation():
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
assert IptcImagePlugin.PAD == b"\0\0\0\0"
|