1
from __future__ import annotations
3
from pathlib import Path
8
from PIL import Image, ImageFile, WmfImagePlugin
10
from .helper import assert_image_similar_tofile, hopper
13
def test_load_raw() -> None:
15
with Image.open("Tests/images/drawing.emf") as im:
16
if hasattr(Image.core, "drawwmf"):
20
assert_image_similar_tofile(im, "Tests/images/drawing_emf_ref.png", 0)
23
with Image.open("Tests/images/drawing.wmf") as im:
24
if hasattr(Image.core, "drawwmf"):
28
assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref.png", 2.0)
31
def test_load() -> None:
32
with Image.open("Tests/images/drawing.emf") as im:
33
if hasattr(Image.core, "drawwmf"):
34
assert im.load()[0, 0] == (255, 255, 255)
37
def test_register_handler(tmp_path: Path) -> None:
38
class TestHandler(ImageFile.StubHandler):
41
def load(self, im: ImageFile.StubImageFile) -> Image.Image:
42
return Image.new("RGB", (1, 1))
44
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
45
self.methodCalled = True
47
handler = TestHandler()
48
original_handler = WmfImagePlugin._handler
49
WmfImagePlugin.register_handler(handler)
52
tmpfile = str(tmp_path / "temp.wmf")
54
assert handler.methodCalled
57
WmfImagePlugin.register_handler(original_handler)
60
def test_load_float_dpi() -> None:
61
with Image.open("Tests/images/drawing.emf") as im:
62
assert im.info["dpi"] == 1423.7668161434979
65
def test_load_set_dpi() -> None:
66
with Image.open("Tests/images/drawing.wmf") as im:
67
assert im.size == (82, 82)
69
if hasattr(Image.core, "drawwmf"):
71
assert im.size == (164, 164)
73
assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref_144.png", 2.1)
76
@pytest.mark.parametrize("ext", (".wmf", ".emf"))
77
def test_save(ext: str, tmp_path: Path) -> None:
80
tmpfile = str(tmp_path / ("temp" + ext))
81
with pytest.raises(OSError):