2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
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
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
from .helper import hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2014-07-30 20:34:20 +04:00
|
|
|
TEST_FILE = "Tests/images/iptc.jpg"
|
|
|
|
|
|
|
|
|
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
|
|
|
|
ret = IptcImagePlugin.i(c)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert ret == 97
|
|
|
|
|
|
|
|
|
|
|
|
def test_dump():
|
|
|
|
# Arrange
|
|
|
|
c = b"abc"
|
|
|
|
# Temporarily redirect stdout
|
|
|
|
old_stdout = sys.stdout
|
|
|
|
sys.stdout = mystdout = StringIO()
|
|
|
|
|
|
|
|
# Act
|
|
|
|
IptcImagePlugin.dump(c)
|
2014-08-01 12:56:21 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Reset stdout
|
|
|
|
sys.stdout = old_stdout
|
2014-08-01 12:56:21 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Assert
|
|
|
|
assert mystdout.getvalue() == "61 62 63 \n"
|