2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2020-02-17 21:23:51 +03:00
|
|
|
import pickle
|
2024-01-31 12:12:58 +03:00
|
|
|
from pathlib import Path
|
2020-02-17 21:23:51 +03:00
|
|
|
|
2020-04-16 11:31:28 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2021-11-10 15:34:20 +03:00
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
2014-04-22 09:54:16 +04:00
|
|
|
|
2021-11-10 15:34:20 +03:00
|
|
|
from .helper import assert_image_equal, skip_unless_feature
|
|
|
|
|
|
|
|
FONT_SIZE = 20
|
|
|
|
FONT_PATH = "Tests/fonts/DejaVuSans/DejaVuSans.ttf"
|
2020-04-16 12:56:12 +03:00
|
|
|
|
2019-05-11 07:01:23 +03:00
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def helper_pickle_file(
|
|
|
|
tmp_path: Path, protocol: int, test_file: str, mode: str | None
|
|
|
|
) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
# Arrange
|
2020-04-16 11:31:28 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-03-22 22:54:54 +03:00
|
|
|
filename = str(tmp_path / "temp.pkl")
|
|
|
|
if mode:
|
|
|
|
im = im.convert(mode)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
with open(filename, "wb") as f:
|
|
|
|
pickle.dump(im, f, protocol)
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
loaded_im = pickle.load(f)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert im == loaded_im
|
|
|
|
|
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def helper_pickle_string(protocol: int, test_file: str, mode: str | None) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
if mode:
|
|
|
|
im = im.convert(mode)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
dumped_string = pickle.dumps(im, protocol)
|
|
|
|
loaded_im = pickle.loads(dumped_string)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert im == loaded_im
|
|
|
|
|
|
|
|
|
2020-04-16 11:31:28 +03:00
|
|
|
@pytest.mark.parametrize(
|
2020-04-16 11:44:28 +03:00
|
|
|
("test_file", "test_mode"),
|
|
|
|
[
|
|
|
|
("Tests/images/hopper.jpg", None),
|
|
|
|
("Tests/images/hopper.jpg", "L"),
|
|
|
|
("Tests/images/hopper.jpg", "PA"),
|
2020-04-16 12:56:12 +03:00
|
|
|
pytest.param(
|
|
|
|
"Tests/images/hopper.webp", None, marks=skip_unless_feature("webp")
|
|
|
|
),
|
2020-04-17 15:13:14 +03:00
|
|
|
("Tests/images/hopper.tif", None),
|
2020-04-16 11:44:28 +03:00
|
|
|
("Tests/images/test-card.png", None),
|
|
|
|
("Tests/images/zero_bb.png", None),
|
|
|
|
("Tests/images/zero_bb_scale2.png", None),
|
|
|
|
("Tests/images/non_zero_bb.png", None),
|
|
|
|
("Tests/images/non_zero_bb_scale2.png", None),
|
|
|
|
("Tests/images/p_trns_single.png", None),
|
|
|
|
("Tests/images/pil123p.png", None),
|
|
|
|
("Tests/images/itxt_chunks.png", None),
|
|
|
|
],
|
2020-04-16 11:31:28 +03:00
|
|
|
)
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("protocol", range(0, pickle.HIGHEST_PROTOCOL + 1))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_pickle_image(
|
|
|
|
tmp_path: Path, test_file: str, test_mode: str | None, protocol: int
|
|
|
|
) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
# Act / Assert
|
2024-02-12 13:06:17 +03:00
|
|
|
helper_pickle_string(protocol, test_file, test_mode)
|
|
|
|
helper_pickle_file(tmp_path, protocol, test_file, test_mode)
|
2020-03-22 22:54:54 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_pickle_la_mode_with_palette(tmp_path: Path) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
# Arrange
|
|
|
|
filename = str(tmp_path / "temp.pkl")
|
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
im = im.convert("PA")
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
|
2023-07-29 02:28:18 +03:00
|
|
|
im._mode = "LA"
|
2020-03-22 22:54:54 +03:00
|
|
|
with open(filename, "wb") as f:
|
|
|
|
pickle.dump(im, f, protocol)
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
loaded_im = pickle.load(f)
|
|
|
|
|
2023-07-29 02:28:18 +03:00
|
|
|
im._mode = "PA"
|
2020-03-22 22:54:54 +03:00
|
|
|
assert im == loaded_im
|
2020-04-17 11:21:57 +03:00
|
|
|
|
|
|
|
|
2020-04-17 13:08:10 +03:00
|
|
|
@skip_unless_feature("webp")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_pickle_tell() -> None:
|
2020-04-17 11:21:57 +03:00
|
|
|
# Arrange
|
2021-11-25 15:16:07 +03:00
|
|
|
with Image.open("Tests/images/hopper.webp") as image:
|
|
|
|
# Act: roundtrip
|
|
|
|
unpickled_image = pickle.loads(pickle.dumps(image))
|
2020-04-17 11:21:57 +03:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert unpickled_image.tell() == 0
|
2021-11-10 15:34:20 +03:00
|
|
|
|
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def helper_assert_pickled_font_images(
|
|
|
|
font1: ImageFont.FreeTypeFont, font2: ImageFont.FreeTypeFont
|
|
|
|
) -> None:
|
2021-11-10 15:34:20 +03:00
|
|
|
# Arrange
|
|
|
|
im1 = Image.new(mode="RGBA", size=(300, 100))
|
|
|
|
im2 = Image.new(mode="RGBA", size=(300, 100))
|
|
|
|
draw1 = ImageDraw.Draw(im1)
|
|
|
|
draw2 = ImageDraw.Draw(im2)
|
|
|
|
txt = "Hello World!"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw1.text((10, 10), txt, font=font1)
|
|
|
|
draw2.text((10, 10), txt, font=font2)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal(im1, im2)
|
|
|
|
|
|
|
|
|
2023-08-24 12:02:27 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2021-11-10 15:34:20 +03:00
|
|
|
@pytest.mark.parametrize("protocol", list(range(0, pickle.HIGHEST_PROTOCOL + 1)))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_pickle_font_string(protocol: int) -> None:
|
2021-11-10 15:34:20 +03:00
|
|
|
# Arrange
|
|
|
|
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
|
|
|
|
|
|
|
# Act: roundtrip
|
|
|
|
pickled_font = pickle.dumps(font, protocol)
|
|
|
|
unpickled_font = pickle.loads(pickled_font)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
helper_assert_pickled_font_images(font, unpickled_font)
|
|
|
|
|
|
|
|
|
2023-08-24 12:02:27 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2021-11-10 15:34:20 +03:00
|
|
|
@pytest.mark.parametrize("protocol", list(range(0, pickle.HIGHEST_PROTOCOL + 1)))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_pickle_font_file(tmp_path: Path, protocol: int) -> None:
|
2021-11-10 15:34:20 +03:00
|
|
|
# Arrange
|
|
|
|
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
|
|
|
filename = str(tmp_path / "temp.pkl")
|
|
|
|
|
|
|
|
# Act: roundtrip
|
|
|
|
with open(filename, "wb") as f:
|
|
|
|
pickle.dump(font, f, protocol)
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
unpickled_font = pickle.load(f)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
helper_assert_pickled_font_images(font, unpickled_font)
|