Pillow/Tests/test_image_split.py

65 lines
2.4 KiB
Python
Raw Normal View History

from PIL import Image
from .helper import PillowTestCase, hopper
2014-06-10 13:10:47 +04:00
class TestImageSplit(PillowTestCase):
def test_split(self):
def split(mode):
layers = hopper(mode).split()
2014-06-10 13:10:47 +04:00
return [(i.mode, i.size[0], i.size[1]) for i in layers]
2019-06-13 18:54:24 +03:00
self.assertEqual(split("1"), [("1", 128, 128)])
self.assertEqual(split("L"), [("L", 128, 128)])
self.assertEqual(split("I"), [("I", 128, 128)])
self.assertEqual(split("F"), [("F", 128, 128)])
self.assertEqual(split("P"), [("P", 128, 128)])
2014-06-10 13:10:47 +04:00
self.assertEqual(
2019-06-13 18:54:24 +03:00
split("RGB"), [("L", 128, 128), ("L", 128, 128), ("L", 128, 128)]
)
2014-06-10 13:10:47 +04:00
self.assertEqual(
split("RGBA"),
2019-06-13 18:54:24 +03:00
[("L", 128, 128), ("L", 128, 128), ("L", 128, 128), ("L", 128, 128)],
)
2014-06-10 13:10:47 +04:00
self.assertEqual(
split("CMYK"),
2019-06-13 18:54:24 +03:00
[("L", 128, 128), ("L", 128, 128), ("L", 128, 128), ("L", 128, 128)],
)
2014-06-10 13:10:47 +04:00
self.assertEqual(
2019-06-13 18:54:24 +03:00
split("YCbCr"), [("L", 128, 128), ("L", 128, 128), ("L", 128, 128)]
)
2014-06-10 13:10:47 +04:00
def test_split_merge(self):
def split_merge(mode):
return Image.merge(mode, hopper(mode).split())
2019-06-13 18:54:24 +03:00
self.assert_image_equal(hopper("1"), split_merge("1"))
self.assert_image_equal(hopper("L"), split_merge("L"))
self.assert_image_equal(hopper("I"), split_merge("I"))
self.assert_image_equal(hopper("F"), split_merge("F"))
self.assert_image_equal(hopper("P"), split_merge("P"))
self.assert_image_equal(hopper("RGB"), split_merge("RGB"))
self.assert_image_equal(hopper("RGBA"), split_merge("RGBA"))
self.assert_image_equal(hopper("CMYK"), split_merge("CMYK"))
self.assert_image_equal(hopper("YCbCr"), split_merge("YCbCr"))
2014-06-10 13:10:47 +04:00
def test_split_open(self):
codecs = dir(Image.core)
2019-06-13 18:54:24 +03:00
if "zip_encoder" in codecs:
2015-04-24 11:24:52 +03:00
test_file = self.tempfile("temp.png")
2014-06-10 13:10:47 +04:00
else:
2015-04-24 11:24:52 +03:00
test_file = self.tempfile("temp.pcx")
2014-06-10 13:10:47 +04:00
def split_open(mode):
2015-04-24 11:24:52 +03:00
hopper(mode).save(test_file)
im = Image.open(test_file)
2014-06-10 13:10:47 +04:00
return len(im.split())
2019-06-13 18:54:24 +03:00
2014-06-10 13:10:47 +04:00
self.assertEqual(split_open("1"), 1)
self.assertEqual(split_open("L"), 1)
self.assertEqual(split_open("P"), 1)
self.assertEqual(split_open("RGB"), 3)
2019-06-13 18:54:24 +03:00
if "zip_encoder" in codecs:
2014-06-10 13:10:47 +04:00
self.assertEqual(split_open("RGBA"), 4)