mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-03-23 19:44:13 +03:00
Convert test_image_putpixel.py. More changes needed for test_cffi.py
This commit is contained in:
parent
3fda42d280
commit
3249d8f3a5
|
@ -1,99 +0,0 @@
|
||||||
from tester import *
|
|
||||||
|
|
||||||
try:
|
|
||||||
import cffi
|
|
||||||
except:
|
|
||||||
skip()
|
|
||||||
|
|
||||||
from PIL import Image, PyAccess
|
|
||||||
|
|
||||||
import test_image_putpixel as put
|
|
||||||
import test_image_getpixel as get
|
|
||||||
|
|
||||||
|
|
||||||
Image.USE_CFFI_ACCESS = True
|
|
||||||
|
|
||||||
def test_put():
|
|
||||||
put.test_sanity()
|
|
||||||
|
|
||||||
def test_get():
|
|
||||||
get.test_basic()
|
|
||||||
get.test_signedness()
|
|
||||||
|
|
||||||
def _test_get_access(im):
|
|
||||||
""" Do we get the same thing as the old pixel access """
|
|
||||||
|
|
||||||
""" Using private interfaces, forcing a capi access and a pyaccess for the same image """
|
|
||||||
caccess = im.im.pixel_access(False)
|
|
||||||
access = PyAccess.new(im, False)
|
|
||||||
|
|
||||||
w,h = im.size
|
|
||||||
for x in range(0,w,10):
|
|
||||||
for y in range(0,h,10):
|
|
||||||
assert_equal(access[(x,y)], caccess[(x,y)])
|
|
||||||
|
|
||||||
def test_get_vs_c():
|
|
||||||
_test_get_access(lena('RGB'))
|
|
||||||
_test_get_access(lena('RGBA'))
|
|
||||||
_test_get_access(lena('L'))
|
|
||||||
_test_get_access(lena('LA'))
|
|
||||||
_test_get_access(lena('1'))
|
|
||||||
_test_get_access(lena('P'))
|
|
||||||
#_test_get_access(lena('PA')) # PA -- how do I make a PA image???
|
|
||||||
_test_get_access(lena('F'))
|
|
||||||
|
|
||||||
im = Image.new('I;16', (10,10), 40000)
|
|
||||||
_test_get_access(im)
|
|
||||||
im = Image.new('I;16L', (10,10), 40000)
|
|
||||||
_test_get_access(im)
|
|
||||||
im = Image.new('I;16B', (10,10), 40000)
|
|
||||||
_test_get_access(im)
|
|
||||||
|
|
||||||
im = Image.new('I', (10,10), 40000)
|
|
||||||
_test_get_access(im)
|
|
||||||
# These don't actually appear to be modes that I can actually make,
|
|
||||||
# as unpack sets them directly into the I mode.
|
|
||||||
#im = Image.new('I;32L', (10,10), -2**10)
|
|
||||||
#_test_get_access(im)
|
|
||||||
#im = Image.new('I;32B', (10,10), 2**10)
|
|
||||||
#_test_get_access(im)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _test_set_access(im, color):
|
|
||||||
""" Are we writing the correct bits into the image? """
|
|
||||||
|
|
||||||
""" Using private interfaces, forcing a capi access and a pyaccess for the same image """
|
|
||||||
caccess = im.im.pixel_access(False)
|
|
||||||
access = PyAccess.new(im, False)
|
|
||||||
|
|
||||||
w,h = im.size
|
|
||||||
for x in range(0,w,10):
|
|
||||||
for y in range(0,h,10):
|
|
||||||
access[(x,y)] = color
|
|
||||||
assert_equal(color, caccess[(x,y)])
|
|
||||||
|
|
||||||
def test_set_vs_c():
|
|
||||||
_test_set_access(lena('RGB'), (255, 128,0) )
|
|
||||||
_test_set_access(lena('RGBA'), (255, 192, 128, 0))
|
|
||||||
_test_set_access(lena('L'), 128)
|
|
||||||
_test_set_access(lena('LA'), (128,128))
|
|
||||||
_test_set_access(lena('1'), 255)
|
|
||||||
_test_set_access(lena('P') , 128)
|
|
||||||
##_test_set_access(i, (128,128)) #PA -- undone how to make
|
|
||||||
_test_set_access(lena('F'), 1024.0)
|
|
||||||
|
|
||||||
im = Image.new('I;16', (10,10), 40000)
|
|
||||||
_test_set_access(im, 45000)
|
|
||||||
im = Image.new('I;16L', (10,10), 40000)
|
|
||||||
_test_set_access(im, 45000)
|
|
||||||
im = Image.new('I;16B', (10,10), 40000)
|
|
||||||
_test_set_access(im, 45000)
|
|
||||||
|
|
||||||
|
|
||||||
im = Image.new('I', (10,10), 40000)
|
|
||||||
_test_set_access(im, 45000)
|
|
||||||
# im = Image.new('I;32L', (10,10), -(2**10))
|
|
||||||
# _test_set_access(im, -(2**13)+1)
|
|
||||||
#im = Image.new('I;32B', (10,10), 2**10)
|
|
||||||
#_test_set_access(im, 2**13-1)
|
|
|
@ -1,28 +0,0 @@
|
||||||
from tester import *
|
|
||||||
|
|
||||||
from PIL import Image
|
|
||||||
from PIL import ImagePalette
|
|
||||||
|
|
||||||
def test_putpalette():
|
|
||||||
def palette(mode):
|
|
||||||
im = lena(mode).copy()
|
|
||||||
im.putpalette(list(range(256))*3)
|
|
||||||
p = im.getpalette()
|
|
||||||
if p:
|
|
||||||
return im.mode, p[:10]
|
|
||||||
return im.mode
|
|
||||||
assert_exception(ValueError, lambda: palette("1"))
|
|
||||||
assert_equal(palette("L"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
|
||||||
assert_equal(palette("P"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
|
||||||
assert_exception(ValueError, lambda: palette("I"))
|
|
||||||
assert_exception(ValueError, lambda: palette("F"))
|
|
||||||
assert_exception(ValueError, lambda: palette("RGB"))
|
|
||||||
assert_exception(ValueError, lambda: palette("RGBA"))
|
|
||||||
assert_exception(ValueError, lambda: palette("YCbCr"))
|
|
||||||
|
|
||||||
def test_imagepalette():
|
|
||||||
im = lena("P")
|
|
||||||
assert_no_exception(lambda: im.putpalette(ImagePalette.negative()))
|
|
||||||
assert_no_exception(lambda: im.putpalette(ImagePalette.random()))
|
|
||||||
assert_no_exception(lambda: im.putpalette(ImagePalette.sepia()))
|
|
||||||
assert_no_exception(lambda: im.putpalette(ImagePalette.wedge()))
|
|
|
@ -1,45 +0,0 @@
|
||||||
from tester import *
|
|
||||||
|
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
Image.USE_CFFI_ACCESS=False
|
|
||||||
|
|
||||||
def test_sanity():
|
|
||||||
|
|
||||||
im1 = lena()
|
|
||||||
im2 = Image.new(im1.mode, im1.size, 0)
|
|
||||||
|
|
||||||
for y in range(im1.size[1]):
|
|
||||||
for x in range(im1.size[0]):
|
|
||||||
pos = x, y
|
|
||||||
im2.putpixel(pos, im1.getpixel(pos))
|
|
||||||
|
|
||||||
assert_image_equal(im1, im2)
|
|
||||||
|
|
||||||
im2 = Image.new(im1.mode, im1.size, 0)
|
|
||||||
im2.readonly = 1
|
|
||||||
|
|
||||||
for y in range(im1.size[1]):
|
|
||||||
for x in range(im1.size[0]):
|
|
||||||
pos = x, y
|
|
||||||
im2.putpixel(pos, im1.getpixel(pos))
|
|
||||||
|
|
||||||
assert_false(im2.readonly)
|
|
||||||
assert_image_equal(im1, im2)
|
|
||||||
|
|
||||||
im2 = Image.new(im1.mode, im1.size, 0)
|
|
||||||
|
|
||||||
pix1 = im1.load()
|
|
||||||
pix2 = im2.load()
|
|
||||||
|
|
||||||
for y in range(im1.size[1]):
|
|
||||||
for x in range(im1.size[0]):
|
|
||||||
pix2[x, y] = pix1[x, y]
|
|
||||||
|
|
||||||
assert_image_equal(im1, im2)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# see test_image_getpixel for more tests
|
|
||||||
|
|
109
test/test_cffi.py
Normal file
109
test/test_cffi.py
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
from helper import unittest, PillowTestCase, lena
|
||||||
|
|
||||||
|
try:
|
||||||
|
import cffi
|
||||||
|
|
||||||
|
from PIL import Image, PyAccess
|
||||||
|
|
||||||
|
import test_image_putpixel as put
|
||||||
|
import test_image_getpixel as get
|
||||||
|
|
||||||
|
class TestCffi(PillowTestCase):
|
||||||
|
|
||||||
|
Image.USE_CFFI_ACCESS = True
|
||||||
|
|
||||||
|
def test_put(self):
|
||||||
|
put.test_sanity()
|
||||||
|
|
||||||
|
def test_get(self):
|
||||||
|
get.test_basic()
|
||||||
|
get.test_signedness()
|
||||||
|
|
||||||
|
def _test_get_access(self, im):
|
||||||
|
""" Do we get the same thing as the old pixel access """
|
||||||
|
|
||||||
|
""" Using private interfaces, forcing a capi access and a pyaccess
|
||||||
|
for the same image """
|
||||||
|
caccess = im.im.pixel_access(False)
|
||||||
|
access = PyAccess.new(im, False)
|
||||||
|
|
||||||
|
w, h = im.size
|
||||||
|
for x in range(0, w, 10):
|
||||||
|
for y in range(0, h, 10):
|
||||||
|
self.assertEqual(access[(x, y)], caccess[(x, y)])
|
||||||
|
|
||||||
|
def test_get_vs_c(self):
|
||||||
|
self._test_get_access(lena('RGB'))
|
||||||
|
self._test_get_access(lena('RGBA'))
|
||||||
|
self._test_get_access(lena('L'))
|
||||||
|
self._test_get_access(lena('LA'))
|
||||||
|
self._test_get_access(lena('1'))
|
||||||
|
self._test_get_access(lena('P'))
|
||||||
|
# PA -- how do I make a PA image???
|
||||||
|
# self._test_get_access(lena('PA'))
|
||||||
|
self._test_get_access(lena('F'))
|
||||||
|
|
||||||
|
im = Image.new('I;16', (10, 10), 40000)
|
||||||
|
self._test_get_access(im)
|
||||||
|
im = Image.new('I;16L', (10, 10), 40000)
|
||||||
|
self._test_get_access(im)
|
||||||
|
im = Image.new('I;16B', (10, 10), 40000)
|
||||||
|
self._test_get_access(im)
|
||||||
|
|
||||||
|
im = Image.new('I', (10, 10), 40000)
|
||||||
|
self._test_get_access(im)
|
||||||
|
# These don't actually appear to be modes that I can actually make,
|
||||||
|
# as unpack sets them directly into the I mode.
|
||||||
|
# im = Image.new('I;32L', (10, 10), -2**10)
|
||||||
|
# self._test_get_access(im)
|
||||||
|
# im = Image.new('I;32B', (10, 10), 2**10)
|
||||||
|
# self._test_get_access(im)
|
||||||
|
|
||||||
|
def _test_set_access(self, im, color):
|
||||||
|
""" Are we writing the correct bits into the image? """
|
||||||
|
|
||||||
|
""" Using private interfaces, forcing a capi access and a pyaccess
|
||||||
|
for the same image """
|
||||||
|
caccess = im.im.pixel_access(False)
|
||||||
|
access = PyAccess.new(im, False)
|
||||||
|
|
||||||
|
w, h = im.size
|
||||||
|
for x in range(0, w, 10):
|
||||||
|
for y in range(0, h, 10):
|
||||||
|
access[(x, y)] = color
|
||||||
|
self.assertEqual(color, caccess[(x, y)])
|
||||||
|
|
||||||
|
def test_set_vs_c(self):
|
||||||
|
self._test_set_access(lena('RGB'), (255, 128, 0))
|
||||||
|
self._test_set_access(lena('RGBA'), (255, 192, 128, 0))
|
||||||
|
self._test_set_access(lena('L'), 128)
|
||||||
|
self._test_set_access(lena('LA'), (128, 128))
|
||||||
|
self._test_set_access(lena('1'), 255)
|
||||||
|
self._test_set_access(lena('P'), 128)
|
||||||
|
# self._test_set_access(i, (128, 128)) #PA -- undone how to make
|
||||||
|
self._test_set_access(lena('F'), 1024.0)
|
||||||
|
|
||||||
|
im = Image.new('I;16', (10, 10), 40000)
|
||||||
|
self._test_set_access(im, 45000)
|
||||||
|
im = Image.new('I;16L', (10, 10), 40000)
|
||||||
|
self._test_set_access(im, 45000)
|
||||||
|
im = Image.new('I;16B', (10, 10), 40000)
|
||||||
|
self._test_set_access(im, 45000)
|
||||||
|
|
||||||
|
im = Image.new('I', (10, 10), 40000)
|
||||||
|
self._test_set_access(im, 45000)
|
||||||
|
# im = Image.new('I;32L', (10, 10), -(2**10))
|
||||||
|
# self._test_set_access(im, -(2**13)+1)
|
||||||
|
# im = Image.new('I;32B', (10, 10), 2**10)
|
||||||
|
# self._test_set_access(im, 2**13-1)
|
||||||
|
|
||||||
|
except ImportError:
|
||||||
|
class TestCffi(PillowTestCase):
|
||||||
|
def test_skip(self):
|
||||||
|
self.skipTest("ImportError")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
# End of file
|
36
test/test_image_putpalette.py
Normal file
36
test/test_image_putpalette.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
from helper import unittest, PillowTestCase, lena
|
||||||
|
|
||||||
|
from PIL import ImagePalette
|
||||||
|
|
||||||
|
|
||||||
|
class TestImage(PillowTestCase):
|
||||||
|
|
||||||
|
def test_putpalette(self):
|
||||||
|
def palette(mode):
|
||||||
|
im = lena(mode).copy()
|
||||||
|
im.putpalette(list(range(256))*3)
|
||||||
|
p = im.getpalette()
|
||||||
|
if p:
|
||||||
|
return im.mode, p[:10]
|
||||||
|
return im.mode
|
||||||
|
self.assertRaises(ValueError, lambda: palette("1"))
|
||||||
|
self.assertEqual(palette("L"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||||
|
self.assertEqual(palette("P"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||||
|
self.assertRaises(ValueError, lambda: palette("I"))
|
||||||
|
self.assertRaises(ValueError, lambda: palette("F"))
|
||||||
|
self.assertRaises(ValueError, lambda: palette("RGB"))
|
||||||
|
self.assertRaises(ValueError, lambda: palette("RGBA"))
|
||||||
|
self.assertRaises(ValueError, lambda: palette("YCbCr"))
|
||||||
|
|
||||||
|
def test_imagepalette(self):
|
||||||
|
im = lena("P")
|
||||||
|
im.putpalette(ImagePalette.negative())
|
||||||
|
im.putpalette(ImagePalette.random())
|
||||||
|
im.putpalette(ImagePalette.sepia())
|
||||||
|
im.putpalette(ImagePalette.wedge())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
# End of file
|
50
test/test_image_putpixel.py
Normal file
50
test/test_image_putpixel.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
from helper import unittest, PillowTestCase, lena
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
Image.USE_CFFI_ACCESS = False
|
||||||
|
|
||||||
|
|
||||||
|
class TestImagePutPixel(PillowTestCase):
|
||||||
|
|
||||||
|
def test_sanity(self):
|
||||||
|
|
||||||
|
im1 = lena()
|
||||||
|
im2 = Image.new(im1.mode, im1.size, 0)
|
||||||
|
|
||||||
|
for y in range(im1.size[1]):
|
||||||
|
for x in range(im1.size[0]):
|
||||||
|
pos = x, y
|
||||||
|
im2.putpixel(pos, im1.getpixel(pos))
|
||||||
|
|
||||||
|
self.assert_image_equal(im1, im2)
|
||||||
|
|
||||||
|
im2 = Image.new(im1.mode, im1.size, 0)
|
||||||
|
im2.readonly = 1
|
||||||
|
|
||||||
|
for y in range(im1.size[1]):
|
||||||
|
for x in range(im1.size[0]):
|
||||||
|
pos = x, y
|
||||||
|
im2.putpixel(pos, im1.getpixel(pos))
|
||||||
|
|
||||||
|
self.assertFalse(im2.readonly)
|
||||||
|
self.assert_image_equal(im1, im2)
|
||||||
|
|
||||||
|
im2 = Image.new(im1.mode, im1.size, 0)
|
||||||
|
|
||||||
|
pix1 = im1.load()
|
||||||
|
pix2 = im2.load()
|
||||||
|
|
||||||
|
for y in range(im1.size[1]):
|
||||||
|
for x in range(im1.size[0]):
|
||||||
|
pix2[x, y] = pix1[x, y]
|
||||||
|
|
||||||
|
self.assert_image_equal(im1, im2)
|
||||||
|
|
||||||
|
# see test_image_getpixel for more tests
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
# End of file
|
Loading…
Reference in New Issue
Block a user