Pillow/Tests/test_image_putdata.py

86 lines
2.4 KiB
Python
Raw Normal View History

from .helper import PillowTestCase, hopper
2014-11-14 01:56:31 +03:00
from array import array
import sys
from PIL import Image
2014-06-10 13:10:47 +04:00
class TestImagePutData(PillowTestCase):
def test_sanity(self):
im1 = hopper()
2014-06-10 13:10:47 +04:00
data = list(im1.getdata())
2014-06-10 13:10:47 +04:00
im2 = Image.new(im1.mode, im1.size, 0)
im2.putdata(data)
2014-06-10 13:10:47 +04:00
self.assert_image_equal(im1, im2)
2014-06-10 13:10:47 +04:00
# readonly
im2 = Image.new(im1.mode, im2.size, 0)
im2.readonly = 1
im2.putdata(data)
2014-06-10 13:10:47 +04:00
self.assertFalse(im2.readonly)
self.assert_image_equal(im1, im2)
def test_long_integers(self):
# 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
2014-06-10 13:10:47 +04:00
self.assertEqual(put(0xFFFFFFFF), (255, 255, 255, 255))
self.assertEqual(put(0xFFFFFFFF), (255, 255, 255, 255))
self.assertEqual(put(-1), (255, 255, 255, 255))
self.assertEqual(put(-1), (255, 255, 255, 255))
2019-06-13 18:54:24 +03:00
if sys.maxsize > 2 ** 32:
2014-06-10 13:10:47 +04:00
self.assertEqual(put(sys.maxsize), (255, 255, 255, 255))
else:
self.assertEqual(put(sys.maxsize), (255, 255, 255, 127))
2014-07-24 02:29:10 +04:00
def test_pypy_performance(self):
2019-06-13 18:54:24 +03:00
im = Image.new("L", (256, 256))
im.putdata(list(range(256)) * 256)
2014-07-24 02:29:10 +04:00
2014-07-29 09:09:52 +04:00
def test_mode_i(self):
2019-06-13 18:54:24 +03:00
src = hopper("L")
2014-07-29 09:09:52 +04:00
data = list(src.getdata())
2019-06-13 18:54:24 +03:00
im = Image.new("I", src.size, 0)
2014-07-29 09:09:52 +04:00
im.putdata(data, 2, 256)
2014-08-28 15:44:19 +04:00
target = [2 * elt + 256 for elt in data]
2014-07-29 09:09:52 +04:00
self.assertEqual(list(im.getdata()), target)
def test_mode_F(self):
2019-06-13 18:54:24 +03:00
src = hopper("L")
2014-07-29 09:09:52 +04:00
data = list(src.getdata())
2019-06-13 18:54:24 +03:00
im = Image.new("F", src.size, 0)
2014-07-29 09:09:52 +04:00
im.putdata(data, 2.0, 256.0)
2014-08-28 15:44:19 +04:00
target = [2.0 * float(elt) + 256.0 for elt in data]
2014-07-29 09:09:52 +04:00
self.assertEqual(list(im.getdata()), target)
2014-11-14 01:56:31 +03:00
def test_array_B(self):
# shouldn't segfault
# see https://github.com/python-pillow/Pillow/issues/1008
2019-06-13 18:54:24 +03:00
arr = array("B", [0]) * 15000
im = Image.new("L", (150, 100))
2014-11-14 01:56:31 +03:00
im.putdata(arr)
2015-04-24 02:26:52 +03:00
self.assertEqual(len(im.getdata()), len(arr))
2014-11-14 01:56:31 +03:00
def test_array_F(self):
# shouldn't segfault
# see https://github.com/python-pillow/Pillow/issues/1008
2019-06-13 18:54:24 +03:00
im = Image.new("F", (150, 100))
arr = array("f", [0.0]) * 15000
2014-11-14 01:56:31 +03:00
im.putdata(arr)
2015-04-24 02:26:52 +03:00
self.assertEqual(len(im.getdata()), len(arr))