2020-02-18 01:03:32 +03:00
|
|
|
from PIL import Image, features
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import PillowTestCase, assert_image_equal, hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
class TestImageSplit(PillowTestCase):
|
|
|
|
def test_split(self):
|
|
|
|
def split(mode):
|
2014-09-05 14:03:56 +04:00
|
|
|
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
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert split("1") == [("1", 128, 128)]
|
|
|
|
assert split("L") == [("L", 128, 128)]
|
|
|
|
assert split("I") == [("I", 128, 128)]
|
|
|
|
assert split("F") == [("F", 128, 128)]
|
|
|
|
assert split("P") == [("P", 128, 128)]
|
|
|
|
assert split("RGB") == [("L", 128, 128), ("L", 128, 128), ("L", 128, 128)]
|
|
|
|
assert split("RGBA") == [
|
|
|
|
("L", 128, 128),
|
|
|
|
("L", 128, 128),
|
|
|
|
("L", 128, 128),
|
|
|
|
("L", 128, 128),
|
|
|
|
]
|
|
|
|
assert split("CMYK") == [
|
|
|
|
("L", 128, 128),
|
|
|
|
("L", 128, 128),
|
|
|
|
("L", 128, 128),
|
|
|
|
("L", 128, 128),
|
|
|
|
]
|
|
|
|
assert 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):
|
2014-09-05 14:03:56 +04:00
|
|
|
return Image.merge(mode, hopper(mode).split())
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(hopper("1"), split_merge("1"))
|
|
|
|
assert_image_equal(hopper("L"), split_merge("L"))
|
|
|
|
assert_image_equal(hopper("I"), split_merge("I"))
|
|
|
|
assert_image_equal(hopper("F"), split_merge("F"))
|
|
|
|
assert_image_equal(hopper("P"), split_merge("P"))
|
|
|
|
assert_image_equal(hopper("RGB"), split_merge("RGB"))
|
|
|
|
assert_image_equal(hopper("RGBA"), split_merge("RGBA"))
|
|
|
|
assert_image_equal(hopper("CMYK"), split_merge("CMYK"))
|
|
|
|
assert_image_equal(hopper("YCbCr"), split_merge("YCbCr"))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_split_open(self):
|
2020-02-18 01:03:32 +03:00
|
|
|
if features.check("zlib"):
|
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)
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
return len(im.split())
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert split_open("1") == 1
|
|
|
|
assert split_open("L") == 1
|
|
|
|
assert split_open("P") == 1
|
|
|
|
assert split_open("RGB") == 3
|
2020-02-18 01:03:32 +03:00
|
|
|
if features.check("zlib"):
|
2020-02-22 16:06:21 +03:00
|
|
|
assert split_open("RGBA") == 4
|