2012-10-16 00:26:38 +04:00
|
|
|
import sys
|
2019-07-06 23:40:53 +03:00
|
|
|
from array import array
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2021-12-28 01:38:10 +03:00
|
|
|
import pytest
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
from .helper import assert_image_equal, hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_sanity():
|
|
|
|
im1 = hopper()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
data = list(im1.getdata())
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im2 = Image.new(im1.mode, im1.size, 0)
|
|
|
|
im2.putdata(data)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_equal(im1, im2)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# readonly
|
|
|
|
im2 = Image.new(im1.mode, im2.size, 0)
|
|
|
|
im2.readonly = 1
|
|
|
|
im2.putdata(data)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
assert not im2.readonly
|
|
|
|
assert_image_equal(im1, im2)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_long_integers():
|
|
|
|
# see bug-200802-systemerror
|
|
|
|
def put(value):
|
|
|
|
im = Image.new("RGBA", (1, 1))
|
|
|
|
im.putdata([value])
|
|
|
|
return im.getpixel((0, 0))
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
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)
|
2023-06-27 07:43:58 +03:00
|
|
|
if sys.maxsize > 2**32:
|
|
|
|
assert put(sys.maxsize) == (255, 255, 255, 255)
|
|
|
|
else:
|
|
|
|
assert put(sys.maxsize) == (255, 255, 255, 127)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-07-24 02:29:10 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_pypy_performance():
|
|
|
|
im = Image.new("L", (256, 256))
|
|
|
|
im.putdata(list(range(256)) * 256)
|
2014-07-29 09:09:52 +04:00
|
|
|
|
|
|
|
|
2021-12-27 09:48:55 +03:00
|
|
|
def test_mode_with_L_with_float():
|
|
|
|
im = Image.new("L", (1, 1), 0)
|
|
|
|
im.putdata([2.0])
|
|
|
|
assert im.getpixel((0, 0)) == 2
|
|
|
|
|
|
|
|
|
2022-12-26 07:46:14 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("I", "I;16", "I;16L", "I;16B"))
|
|
|
|
def test_mode_i(mode):
|
2020-02-12 19:29:19 +03:00
|
|
|
src = hopper("L")
|
|
|
|
data = list(src.getdata())
|
2022-12-26 07:46:14 +03:00
|
|
|
im = Image.new(mode, src.size, 0)
|
2020-02-12 19:29:19 +03:00
|
|
|
im.putdata(data, 2, 256)
|
2014-07-29 09:09:52 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
target = [2 * elt + 256 for elt in data]
|
|
|
|
assert list(im.getdata()) == target
|
2014-07-29 09:09:52 +04:00
|
|
|
|
2014-11-14 01:56:31 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_mode_F():
|
|
|
|
src = hopper("L")
|
|
|
|
data = list(src.getdata())
|
|
|
|
im = Image.new("F", src.size, 0)
|
|
|
|
im.putdata(data, 2.0, 256.0)
|
2014-11-14 01:56:31 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
target = [2.0 * float(elt) + 256.0 for elt in data]
|
|
|
|
assert list(im.getdata()) == target
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2014-11-14 01:56:31 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_array_B():
|
|
|
|
# shouldn't segfault
|
|
|
|
# see https://github.com/python-pillow/Pillow/issues/1008
|
2014-11-14 01:56:31 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
arr = array("B", [0]) * 15000
|
|
|
|
im = Image.new("L", (150, 100))
|
|
|
|
im.putdata(arr)
|
|
|
|
|
|
|
|
assert len(im.getdata()) == len(arr)
|
|
|
|
|
|
|
|
|
|
|
|
def test_array_F():
|
|
|
|
# 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.getdata()) == len(arr)
|
2021-12-28 01:38:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_not_flattened():
|
|
|
|
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]])
|