Pillow

Форк
0
/
test_file_wmf.py 
82 строки · 2.4 Кб
1
from __future__ import annotations
2

3
from pathlib import Path
4
from typing import IO
5

6
import pytest
7

8
from PIL import Image, ImageFile, WmfImagePlugin
9

10
from .helper import assert_image_similar_tofile, hopper
11

12

13
def test_load_raw() -> None:
14
    # Test basic EMF open and rendering
15
    with Image.open("Tests/images/drawing.emf") as im:
16
        if hasattr(Image.core, "drawwmf"):
17
            # Currently, support for WMF/EMF is Windows-only
18
            im.load()
19
            # Compare to reference rendering
20
            assert_image_similar_tofile(im, "Tests/images/drawing_emf_ref.png", 0)
21

22
    # Test basic WMF open and rendering
23
    with Image.open("Tests/images/drawing.wmf") as im:
24
        if hasattr(Image.core, "drawwmf"):
25
            # Currently, support for WMF/EMF is Windows-only
26
            im.load()
27
            # Compare to reference rendering
28
            assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref.png", 2.0)
29

30

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)
35

36

37
def test_register_handler(tmp_path: Path) -> None:
38
    class TestHandler(ImageFile.StubHandler):
39
        methodCalled = False
40

41
        def load(self, im: ImageFile.StubImageFile) -> Image.Image:
42
            return Image.new("RGB", (1, 1))
43

44
        def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
45
            self.methodCalled = True
46

47
    handler = TestHandler()
48
    original_handler = WmfImagePlugin._handler
49
    WmfImagePlugin.register_handler(handler)
50

51
    im = hopper()
52
    tmpfile = str(tmp_path / "temp.wmf")
53
    im.save(tmpfile)
54
    assert handler.methodCalled
55

56
    # Restore the state before this test
57
    WmfImagePlugin.register_handler(original_handler)
58

59

60
def test_load_float_dpi() -> None:
61
    with Image.open("Tests/images/drawing.emf") as im:
62
        assert im.info["dpi"] == 1423.7668161434979
63

64

65
def test_load_set_dpi() -> None:
66
    with Image.open("Tests/images/drawing.wmf") as im:
67
        assert im.size == (82, 82)
68

69
        if hasattr(Image.core, "drawwmf"):
70
            im.load(144)
71
            assert im.size == (164, 164)
72

73
            assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref_144.png", 2.1)
74

75

76
@pytest.mark.parametrize("ext", (".wmf", ".emf"))
77
def test_save(ext: str, tmp_path: Path) -> None:
78
    im = hopper()
79

80
    tmpfile = str(tmp_path / ("temp" + ext))
81
    with pytest.raises(OSError):
82
        im.save(tmpfile)
83

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.