2019-01-13 20:00:12 +03:00
|
|
|
from . import helper
|
2019-02-03 18:34:53 +03:00
|
|
|
from .helper import PillowTestCase
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-11-07 11:48:47 +03:00
|
|
|
from PIL.Image import (FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM, ROTATE_90, ROTATE_180,
|
2017-09-11 23:00:35 +03:00
|
|
|
ROTATE_270, TRANSPOSE, TRANSVERSE)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImageTranspose(PillowTestCase):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-11-07 13:15:51 +03:00
|
|
|
hopper = {
|
2015-04-24 11:24:52 +03:00
|
|
|
'L': helper.hopper('L').crop((0, 0, 121, 127)).copy(),
|
|
|
|
'RGB': helper.hopper('RGB').crop((0, 0, 121, 127)).copy(),
|
2014-11-07 13:15:51 +03:00
|
|
|
}
|
|
|
|
|
2014-11-07 11:48:47 +03:00
|
|
|
def test_flip_left_right(self):
|
|
|
|
def transpose(mode):
|
2014-11-07 13:15:51 +03:00
|
|
|
im = self.hopper[mode]
|
2014-11-07 11:48:47 +03:00
|
|
|
out = im.transpose(FLIP_LEFT_RIGHT)
|
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, im.size)
|
|
|
|
|
|
|
|
x, y = im.size
|
|
|
|
self.assertEqual(im.getpixel((1, 1)), out.getpixel((x-2, 1)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((1, 1)))
|
|
|
|
self.assertEqual(im.getpixel((1, y-2)), out.getpixel((x-2, y-2)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((1, y-2)))
|
|
|
|
|
|
|
|
for mode in ("L", "RGB"):
|
|
|
|
transpose(mode)
|
|
|
|
|
|
|
|
def test_flip_top_bottom(self):
|
|
|
|
def transpose(mode):
|
2014-11-07 13:15:51 +03:00
|
|
|
im = self.hopper[mode]
|
2014-11-07 11:48:47 +03:00
|
|
|
out = im.transpose(FLIP_TOP_BOTTOM)
|
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, im.size)
|
|
|
|
|
|
|
|
x, y = im.size
|
|
|
|
self.assertEqual(im.getpixel((1, 1)), out.getpixel((1, y-2)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((x-2, y-2)))
|
|
|
|
self.assertEqual(im.getpixel((1, y-2)), out.getpixel((1, 1)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((x-2, 1)))
|
|
|
|
|
|
|
|
for mode in ("L", "RGB"):
|
|
|
|
transpose(mode)
|
|
|
|
|
|
|
|
def test_rotate_90(self):
|
|
|
|
def transpose(mode):
|
2014-11-07 13:15:51 +03:00
|
|
|
im = self.hopper[mode]
|
2014-11-07 11:48:47 +03:00
|
|
|
out = im.transpose(ROTATE_90)
|
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, im.size[::-1])
|
|
|
|
|
|
|
|
x, y = im.size
|
2014-11-07 13:15:51 +03:00
|
|
|
self.assertEqual(im.getpixel((1, 1)), out.getpixel((1, x-2)))
|
2014-11-07 11:48:47 +03:00
|
|
|
self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((1, 1)))
|
2014-11-07 13:15:51 +03:00
|
|
|
self.assertEqual(im.getpixel((1, y-2)), out.getpixel((y-2, x-2)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((y-2, 1)))
|
2014-11-07 11:48:47 +03:00
|
|
|
|
|
|
|
for mode in ("L", "RGB"):
|
|
|
|
transpose(mode)
|
|
|
|
|
|
|
|
def test_rotate_180(self):
|
|
|
|
def transpose(mode):
|
2014-11-07 13:15:51 +03:00
|
|
|
im = self.hopper[mode]
|
2014-11-07 11:48:47 +03:00
|
|
|
out = im.transpose(ROTATE_180)
|
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, im.size)
|
|
|
|
|
|
|
|
x, y = im.size
|
|
|
|
self.assertEqual(im.getpixel((1, 1)), out.getpixel((x-2, y-2)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((1, y-2)))
|
|
|
|
self.assertEqual(im.getpixel((1, y-2)), out.getpixel((x-2, 1)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((1, 1)))
|
|
|
|
|
|
|
|
for mode in ("L", "RGB"):
|
|
|
|
transpose(mode)
|
|
|
|
|
|
|
|
def test_rotate_270(self):
|
|
|
|
def transpose(mode):
|
2014-11-07 13:15:51 +03:00
|
|
|
im = self.hopper[mode]
|
2014-11-07 11:48:47 +03:00
|
|
|
out = im.transpose(ROTATE_270)
|
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, im.size[::-1])
|
|
|
|
|
|
|
|
x, y = im.size
|
2014-11-07 13:15:51 +03:00
|
|
|
self.assertEqual(im.getpixel((1, 1)), out.getpixel((y-2, 1)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((y-2, x-2)))
|
2014-11-07 11:48:47 +03:00
|
|
|
self.assertEqual(im.getpixel((1, y-2)), out.getpixel((1, 1)))
|
2014-11-07 13:15:51 +03:00
|
|
|
self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((1, x-2)))
|
2014-11-07 11:48:47 +03:00
|
|
|
|
|
|
|
for mode in ("L", "RGB"):
|
|
|
|
transpose(mode)
|
|
|
|
|
|
|
|
def test_transpose(self):
|
|
|
|
def transpose(mode):
|
2014-11-07 13:15:51 +03:00
|
|
|
im = self.hopper[mode]
|
2014-11-07 11:48:47 +03:00
|
|
|
out = im.transpose(TRANSPOSE)
|
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, im.size[::-1])
|
|
|
|
|
|
|
|
x, y = im.size
|
|
|
|
self.assertEqual(im.getpixel((1, 1)), out.getpixel((1, 1)))
|
2014-11-07 13:15:51 +03:00
|
|
|
self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((1, x-2)))
|
|
|
|
self.assertEqual(im.getpixel((1, y-2)), out.getpixel((y-2, 1)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((y-2, x-2)))
|
2014-11-07 11:48:47 +03:00
|
|
|
|
|
|
|
for mode in ("L", "RGB"):
|
|
|
|
transpose(mode)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2017-09-11 23:00:35 +03:00
|
|
|
def test_tranverse(self):
|
|
|
|
def transpose(mode):
|
|
|
|
im = self.hopper[mode]
|
|
|
|
out = im.transpose(TRANSVERSE)
|
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, im.size[::-1])
|
|
|
|
|
|
|
|
x, y = im.size
|
|
|
|
self.assertEqual(im.getpixel((1, 1)), out.getpixel((y-2, x-2)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((y-2, 1)))
|
|
|
|
self.assertEqual(im.getpixel((1, y-2)), out.getpixel((1, x-2)))
|
|
|
|
self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((1, 1)))
|
|
|
|
|
|
|
|
for mode in ("L", "RGB"):
|
|
|
|
transpose(mode)
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_roundtrip(self):
|
2014-11-07 13:15:51 +03:00
|
|
|
im = self.hopper['L']
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def transpose(first, second):
|
|
|
|
return im.transpose(first).transpose(second)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_equal(
|
|
|
|
im, transpose(FLIP_LEFT_RIGHT, FLIP_LEFT_RIGHT))
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, transpose(FLIP_TOP_BOTTOM, FLIP_TOP_BOTTOM))
|
|
|
|
self.assert_image_equal(im, transpose(ROTATE_90, ROTATE_270))
|
|
|
|
self.assert_image_equal(im, transpose(ROTATE_180, ROTATE_180))
|
2014-11-07 11:48:47 +03:00
|
|
|
self.assert_image_equal(
|
|
|
|
im.transpose(TRANSPOSE), transpose(ROTATE_90, FLIP_TOP_BOTTOM))
|
|
|
|
self.assert_image_equal(
|
|
|
|
im.transpose(TRANSPOSE), transpose(ROTATE_270, FLIP_LEFT_RIGHT))
|
2017-09-11 23:00:35 +03:00
|
|
|
self.assert_image_equal(
|
|
|
|
im.transpose(TRANSVERSE), transpose(ROTATE_90, FLIP_LEFT_RIGHT))
|
|
|
|
self.assert_image_equal(
|
|
|
|
im.transpose(TRANSVERSE), transpose(ROTATE_270, FLIP_TOP_BOTTOM))
|
|
|
|
self.assert_image_equal(
|
|
|
|
im.transpose(TRANSVERSE), transpose(ROTATE_180, TRANSPOSE))
|