Pillow

Форк
0
/
test_imagetk.py 
114 строк · 2.8 Кб
1
from __future__ import annotations
2

3
import pytest
4

5
from PIL import Image
6

7
from .helper import assert_image_equal, hopper
8

9
try:
10
    import tkinter as tk
11

12
    from PIL import ImageTk
13

14
    dir(ImageTk)
15
    HAS_TK = True
16
except (OSError, ImportError):
17
    # Skipped via pytestmark
18
    HAS_TK = False
19

20
TK_MODES = ("1", "L", "P", "RGB", "RGBA")
21

22

23
pytestmark = pytest.mark.skipif(not HAS_TK, reason="Tk not installed")
24

25

26
def setup_module() -> None:
27
    try:
28
        # setup tk
29
        tk.Frame()
30
        # root = tk.Tk()
31
    except RuntimeError as v:
32
        pytest.skip(f"RuntimeError: {v}")
33
    except tk.TclError as v:
34
        pytest.skip(f"TCL Error: {v}")
35

36

37
def test_kw() -> None:
38
    TEST_JPG = "Tests/images/hopper.jpg"
39
    TEST_PNG = "Tests/images/hopper.png"
40
    with Image.open(TEST_JPG) as im1:
41
        with Image.open(TEST_PNG) as im2:
42
            with open(TEST_PNG, "rb") as fp:
43
                data = fp.read()
44
            kw = {"file": TEST_JPG, "data": data}
45

46
            # Test "file"
47
            im = ImageTk._get_image_from_kw(kw)
48
            assert im is not None
49
            assert_image_equal(im, im1)
50

51
            # Test "data"
52
            im = ImageTk._get_image_from_kw(kw)
53
            assert im is not None
54
            assert_image_equal(im, im2)
55

56
    # Test no relevant entry
57
    im = ImageTk._get_image_from_kw(kw)
58
    assert im is None
59

60

61
@pytest.mark.parametrize("mode", TK_MODES)
62
def test_photoimage(mode: str) -> None:
63
    # test as image:
64
    im = hopper(mode)
65

66
    # this should not crash
67
    im_tk = ImageTk.PhotoImage(im)
68

69
    assert im_tk.width() == im.width
70
    assert im_tk.height() == im.height
71

72
    reloaded = ImageTk.getimage(im_tk)
73
    assert_image_equal(reloaded, im.convert("RGBA"))
74

75
    with pytest.raises(ValueError):
76
        ImageTk.PhotoImage()
77
    with pytest.raises(ValueError):
78
        ImageTk.PhotoImage(mode)
79

80

81
def test_photoimage_apply_transparency() -> None:
82
    with Image.open("Tests/images/pil123p.png") as im:
83
        im_tk = ImageTk.PhotoImage(im)
84
        reloaded = ImageTk.getimage(im_tk)
85
        assert_image_equal(reloaded, im.convert("RGBA"))
86

87

88
@pytest.mark.parametrize("mode", TK_MODES)
89
def test_photoimage_blank(mode: str) -> None:
90
    # test a image using mode/size:
91
    im_tk = ImageTk.PhotoImage(mode, (100, 100))
92

93
    assert im_tk.width() == 100
94
    assert im_tk.height() == 100
95

96
    im = Image.new(mode, (100, 100))
97
    reloaded = ImageTk.getimage(im_tk)
98
    assert_image_equal(reloaded.convert(mode), im)
99

100

101
def test_bitmapimage() -> None:
102
    im = hopper("1")
103

104
    # this should not crash
105
    im_tk = ImageTk.BitmapImage(im)
106

107
    assert im_tk.width() == im.width
108
    assert im_tk.height() == im.height
109

110
    # reloaded = ImageTk.getimage(im_tk)
111
    # assert_image_equal(reloaded, im)
112

113
    with pytest.raises(ValueError):
114
        ImageTk.BitmapImage()
115

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

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

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

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