File: test_image_putdata.py

package info (click to toggle)
pillow 12.1.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 72,624 kB
  • sloc: python: 49,768; ansic: 38,750; makefile: 302; sh: 169; javascript: 85
file content (114 lines) | stat: -rw-r--r-- 2,948 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from __future__ import annotations

import sys
from array import array
from typing import cast

import pytest

from PIL import Image

from .helper import assert_image_equal, hopper


def test_sanity() -> None:
    im1 = hopper()
    for data in (im1.get_flattened_data(), im1.im):
        im2 = Image.new(im1.mode, im1.size, 0)
        im2.putdata(data)

        assert_image_equal(im1, im2)

        # readonly
        im2 = Image.new(im1.mode, im2.size, 0)
        im2.readonly = 1
        im2.putdata(data)

        assert not im2.readonly
        assert_image_equal(im1, im2)


def test_long_integers() -> None:
    # see bug-200802-systemerror
    def put(value: int) -> float | tuple[int, ...] | None:
        im = Image.new("RGBA", (1, 1))
        im.putdata([value])
        return im.getpixel((0, 0))

    assert put(0xFFFFFFFF) == (255, 255, 255, 255)
    assert put(0xFFFFFFFF) == (255, 255, 255, 255)
    assert put(-1) == (255, 255, 255, 255)
    assert put(-1) == (255, 255, 255, 255)
    if sys.maxsize > 2**32:
        assert put(sys.maxsize) == (255, 255, 255, 255)
    else:
        assert put(sys.maxsize) == (255, 255, 255, 127)


def test_pypy_performance() -> None:
    im = Image.new("L", (256, 256))
    im.putdata(list(range(256)) * 256)


def test_mode_with_L_with_float() -> None:
    im = Image.new("L", (1, 1), 0)
    im.putdata([2.0])
    assert im.getpixel((0, 0)) == 2


@pytest.mark.parametrize("mode", ("I", "I;16", "I;16L", "I;16B"))
def test_mode_i(mode: str) -> None:
    src = hopper("L")
    data = src.get_flattened_data()
    im = Image.new(mode, src.size, 0)
    im.putdata(data, 2, 256)

    target = tuple(2 * elt + 256 for elt in cast(tuple[int, ...], data))
    assert im.get_flattened_data() == target


def test_mode_F() -> None:
    src = hopper("L")
    data = src.get_flattened_data()
    im = Image.new("F", src.size, 0)
    im.putdata(data, 2.0, 256.0)

    target = tuple(2.0 * float(elt) + 256.0 for elt in cast(tuple[int, ...], data))
    assert im.get_flattened_data() == target


def test_array_B() -> None:
    # shouldn't segfault
    # see https://github.com/python-pillow/Pillow/issues/1008

    arr = array("B", [0]) * 15000
    im = Image.new("L", (150, 100))
    im.putdata(arr)

    assert len(im.get_flattened_data()) == len(arr)


def test_array_F() -> None:
    # shouldn't segfault
    # see https://github.com/python-pillow/Pillow/issues/1008

    im = Image.new("F", (150, 100))
    arr = array("f", [0.0]) * 15000
    im.putdata(arr)

    assert len(im.get_flattened_data()) == len(arr)


def test_not_flattened() -> None:
    im = Image.new("L", (1, 1))
    with pytest.raises(TypeError):
        im.putdata([[0]])
    with pytest.raises(TypeError):
        im.putdata([[0]], 2)

    with pytest.raises(TypeError):
        im = Image.new("I", (1, 1))
        im.putdata([[0]])
    with pytest.raises(TypeError):
        im = Image.new("F", (1, 1))
        im.putdata([[0]])