File: test_image_draft.py

package info (click to toggle)
pillow 12.1.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 72,560 kB
  • sloc: python: 49,748; ansic: 38,748; makefile: 302; sh: 168; javascript: 85
file content (81 lines) | stat: -rw-r--r-- 2,673 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from __future__ import annotations

from PIL import Image

from .helper import fromstring, skip_unless_feature, tostring

pytestmark = skip_unless_feature("jpg")


def draft_roundtrip(
    in_mode: str,
    in_size: tuple[int, int],
    req_mode: str | None,
    req_size: tuple[int, int] | None,
) -> Image.Image:
    im = Image.new(in_mode, in_size)
    data = tostring(im, "JPEG")
    im = fromstring(data)
    result = im.draft(req_mode, req_size)
    assert result is not None
    box = result[1]
    scale, _ = im.decoderconfig
    assert box[:2] == (0, 0)
    assert (im.width - scale) < box[2] <= im.width
    assert (im.height - scale) < box[3] <= im.height
    return im


def test_size() -> None:
    for in_size, req_size, out_size in [
        ((435, 361), (2048, 2048), (435, 361)),  # bigger
        ((435, 361), (435, 361), (435, 361)),  # same
        ((128, 128), (64, 64), (64, 64)),
        ((128, 128), (32, 32), (32, 32)),
        ((128, 128), (16, 16), (16, 16)),
        # large requested width
        ((435, 361), (218, 128), (435, 361)),  # almost 2x
        ((435, 361), (217, 128), (218, 181)),  # more than 2x
        ((435, 361), (109, 64), (218, 181)),  # almost 4x
        ((435, 361), (108, 64), (109, 91)),  # more than 4x
        ((435, 361), (55, 32), (109, 91)),  # almost 8x
        ((435, 361), (54, 32), (55, 46)),  # more than 8x
        ((435, 361), (27, 16), (55, 46)),  # more than 16x
        # and vice versa
        ((435, 361), (128, 181), (435, 361)),  # almost 2x
        ((435, 361), (128, 180), (218, 181)),  # more than 2x
        ((435, 361), (64, 91), (218, 181)),  # almost 4x
        ((435, 361), (64, 90), (109, 91)),  # more than 4x
        ((435, 361), (32, 46), (109, 91)),  # almost 8x
        ((435, 361), (32, 45), (55, 46)),  # more than 8x
        ((435, 361), (16, 22), (55, 46)),  # more than 16x
    ]:
        im = draft_roundtrip("L", in_size, None, req_size)
        im.load()
        assert im.size == out_size


def test_mode() -> None:
    for in_mode, req_mode, out_mode in [
        ("RGB", "1", "RGB"),
        ("RGB", "L", "L"),
        ("RGB", "RGB", "RGB"),
        ("RGB", "YCbCr", "YCbCr"),
        ("L", "1", "L"),
        ("L", "L", "L"),
        ("L", "RGB", "L"),
        ("L", "YCbCr", "L"),
        ("CMYK", "1", "CMYK"),
        ("CMYK", "L", "CMYK"),
        ("CMYK", "RGB", "CMYK"),
        ("CMYK", "YCbCr", "CMYK"),
    ]:
        im = draft_roundtrip(in_mode, (64, 64), req_mode, None)
        im.load()
        assert im.mode == out_mode


def test_several_drafts() -> None:
    im = draft_roundtrip("L", (128, 128), None, (64, 64))
    im.draft(None, (64, 64))
    im.load()