2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2024-10-19 12:40:13 +03:00
|
|
|
from io import BytesIO
|
2024-01-31 12:12:58 +03:00
|
|
|
from pathlib import Path
|
2024-06-05 15:27:23 +03:00
|
|
|
from typing import IO
|
2024-01-31 12:12:58 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2024-06-05 15:27:23 +03:00
|
|
|
from PIL import Image, ImageFile, WmfImagePlugin
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
from .helper import assert_image_similar_tofile, hopper
|
2020-03-28 04:51:28 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_load_raw() -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
# Test basic EMF open and rendering
|
|
|
|
with Image.open("Tests/images/drawing.emf") as im:
|
|
|
|
if hasattr(Image.core, "drawwmf"):
|
|
|
|
# Currently, support for WMF/EMF is Windows-only
|
|
|
|
im.load()
|
|
|
|
# Compare to reference rendering
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/drawing_emf_ref.png", 0)
|
2020-03-28 04:51:28 +03:00
|
|
|
|
|
|
|
# Test basic WMF open and rendering
|
|
|
|
with Image.open("Tests/images/drawing.wmf") as im:
|
|
|
|
if hasattr(Image.core, "drawwmf"):
|
|
|
|
# Currently, support for WMF/EMF is Windows-only
|
|
|
|
im.load()
|
|
|
|
# Compare to reference rendering
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref.png", 2.0)
|
2020-03-28 04:51:28 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_load() -> None:
|
2022-02-02 03:49:31 +03:00
|
|
|
with Image.open("Tests/images/drawing.emf") as im:
|
|
|
|
if hasattr(Image.core, "drawwmf"):
|
|
|
|
assert im.load()[0, 0] == (255, 255, 255)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_register_handler(tmp_path: Path) -> None:
|
2024-06-05 15:27:23 +03:00
|
|
|
class TestHandler(ImageFile.StubHandler):
|
2020-03-28 04:51:28 +03:00
|
|
|
methodCalled = False
|
|
|
|
|
2024-06-05 15:27:23 +03:00
|
|
|
def load(self, im: ImageFile.StubImageFile) -> Image.Image:
|
|
|
|
return Image.new("RGB", (1, 1))
|
|
|
|
|
|
|
|
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
self.methodCalled = True
|
|
|
|
|
|
|
|
handler = TestHandler()
|
2020-03-27 14:15:04 +03:00
|
|
|
original_handler = WmfImagePlugin._handler
|
2020-03-28 04:51:28 +03:00
|
|
|
WmfImagePlugin.register_handler(handler)
|
|
|
|
|
|
|
|
im = hopper()
|
|
|
|
tmpfile = str(tmp_path / "temp.wmf")
|
|
|
|
im.save(tmpfile)
|
|
|
|
assert handler.methodCalled
|
|
|
|
|
|
|
|
# Restore the state before this test
|
2020-03-27 14:15:04 +03:00
|
|
|
WmfImagePlugin.register_handler(original_handler)
|
2020-03-28 04:51:28 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_load_float_dpi() -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
with Image.open("Tests/images/drawing.emf") as im:
|
2021-05-06 13:08:54 +03:00
|
|
|
assert im.info["dpi"] == 1423.7668161434979
|
2020-03-28 04:51:28 +03:00
|
|
|
|
2024-10-19 12:40:13 +03:00
|
|
|
with open("Tests/images/drawing.emf", "rb") as fp:
|
|
|
|
data = fp.read()
|
|
|
|
b = BytesIO(data[:8] + b"\x06\xFA" + data[10:])
|
|
|
|
with Image.open(b) as im:
|
|
|
|
assert im.info["dpi"][0] == 2540
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_load_set_dpi() -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
with Image.open("Tests/images/drawing.wmf") as im:
|
|
|
|
assert im.size == (82, 82)
|
|
|
|
|
|
|
|
if hasattr(Image.core, "drawwmf"):
|
|
|
|
im.load(144)
|
|
|
|
assert im.size == (164, 164)
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref_144.png", 2.1)
|
2020-03-28 04:51:28 +03:00
|
|
|
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("ext", (".wmf", ".emf"))
|
2024-06-05 15:27:23 +03:00
|
|
|
def test_save(ext: str, tmp_path: Path) -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
im = hopper()
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
tmpfile = str(tmp_path / ("temp" + ext))
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.save(tmpfile)
|