Pillow

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

3
import os
4
import warnings
5

6
from PIL import Image
7

8
from .helper import assert_image_similar
9

10
base = os.path.join("Tests", "images", "bmp")
11

12

13
def get_files(d: str, ext: str = ".bmp") -> list[str]:
14
    return [
15
        os.path.join(base, d, f) for f in os.listdir(os.path.join(base, d)) if ext in f
16
    ]
17

18

19
def test_bad() -> None:
20
    """These shouldn't crash/dos, but they shouldn't return anything
21
    either"""
22
    for f in get_files("b"):
23
        # Assert that there is no unclosed file warning
24
        with warnings.catch_warnings():
25
            try:
26
                with Image.open(f) as im:
27
                    im.load()
28
            except Exception:  # as msg:
29
                pass
30

31

32
def test_questionable() -> None:
33
    """These shouldn't crash/dos, but it's not well defined that these
34
    are in spec"""
35
    supported = [
36
        "pal8os2v2.bmp",
37
        "rgb24prof.bmp",
38
        "pal1p1.bmp",
39
        "pal4rletrns.bmp",
40
        "pal8offs.bmp",
41
        "rgb24lprof.bmp",
42
        "rgb32fakealpha.bmp",
43
        "rgb24largepal.bmp",
44
        "pal8os2sp.bmp",
45
        "pal8rletrns.bmp",
46
        "rgb32bf-xbgr.bmp",
47
        "rgba32.bmp",
48
        "rgb32h52.bmp",
49
        "rgba32h56.bmp",
50
    ]
51
    for f in get_files("q"):
52
        try:
53
            with Image.open(f) as im:
54
                im.load()
55
            if os.path.basename(f) not in supported:
56
                print(f"Please add {f} to the partially supported bmp specs.")
57
        except Exception:  # as msg:
58
            if os.path.basename(f) in supported:
59
                raise
60

61

62
def test_good() -> None:
63
    """These should all work. There's a set of target files in the
64
    html directory that we can compare against."""
65

66
    # Target files, if they're not just replacing the extension
67
    file_map = {
68
        "pal1wb.bmp": "pal1.png",
69
        "pal4rle.bmp": "pal4.png",
70
        "pal8-0.bmp": "pal8.png",
71
        "pal8rle.bmp": "pal8.png",
72
        "pal8topdown.bmp": "pal8.png",
73
        "pal8nonsquare.bmp": "pal8nonsquare-v.png",
74
        "pal8os2.bmp": "pal8.png",
75
        "pal8os2sp.bmp": "pal8.png",
76
        "pal8os2v2.bmp": "pal8.png",
77
        "pal8os2v2-16.bmp": "pal8.png",
78
        "pal8v4.bmp": "pal8.png",
79
        "pal8v5.bmp": "pal8.png",
80
        "rgb16-565pal.bmp": "rgb16-565.png",
81
        "rgb24pal.bmp": "rgb24.png",
82
        "rgb32.bmp": "rgb24.png",
83
        "rgb32bf.bmp": "rgb24.png",
84
    }
85

86
    def get_compare(f: str) -> str:
87
        name = os.path.split(f)[1]
88
        if name in file_map:
89
            return os.path.join(base, "html", file_map[name])
90
        name = os.path.splitext(name)[0]
91
        return os.path.join(base, "html", f"{name}.png")
92

93
    for f in get_files("g"):
94
        try:
95
            with Image.open(f) as im:
96
                im.load()
97
                with Image.open(get_compare(f)) as compare:
98
                    compare.load()
99
                    if im.mode == "P":
100
                        # assert image similar doesn't really work
101
                        # with paletized image, since the palette might
102
                        # be differently ordered for an equivalent image.
103
                        im = im.convert("RGBA")
104
                        compare = im.convert("RGBA")
105
                    assert_image_similar(im, compare, 5)
106

107
        except Exception as msg:
108
            # there are three here that are unsupported:
109
            unsupported = (
110
                os.path.join(base, "g", "rgb32bf.bmp"),
111
                os.path.join(base, "g", "pal8rle.bmp"),
112
                os.path.join(base, "g", "pal4rle.bmp"),
113
            )
114
            assert f in unsupported, f"Unsupported Image {f}: {msg}"
115

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

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

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

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