Pillow/Tests/test_image_convert.py

131 lines
3.0 KiB
Python
Raw Normal View History

from tester import *
from PIL import Image
2014-05-18 00:33:50 +04:00
def test_sanity():
def convert(im, mode):
out = im.convert(mode)
assert_equal(out.mode, mode)
assert_equal(out.size, im.size)
modes = "1", "L", "I", "F", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr"
for mode in modes:
im = lena(mode)
for mode in modes:
yield_test(convert, im, mode)
2014-05-18 00:33:50 +04:00
def test_default():
im = lena("P")
assert_image(im, "P", im.size)
im = im.convert()
assert_image(im, "RGB", im.size)
im = im.convert()
assert_image(im, "RGB", im.size)
# ref https://github.com/python-pillow/Pillow/issues/274
def _test_float_conversion(im):
2014-05-18 00:33:50 +04:00
orig = im.getpixel((5, 5))
converted = im.convert('F').getpixel((5, 5))
assert_equal(orig, converted)
2014-05-18 00:33:50 +04:00
def test_8bit():
im = Image.open('Images/lena.jpg')
_test_float_conversion(im.convert('L'))
2014-05-18 00:33:50 +04:00
def test_16bit():
im = Image.open('Tests/images/16bit.cropped.tif')
_test_float_conversion(im)
2014-05-18 00:33:50 +04:00
def test_16bit_workaround():
im = Image.open('Tests/images/16bit.cropped.tif')
_test_float_conversion(im.convert('I'))
2014-05-17 20:08:08 +04:00
2014-05-18 00:33:50 +04:00
2014-03-26 08:42:04 +04:00
def test_rgba_p():
im = lena('RGBA')
im.putalpha(lena('L'))
2014-03-26 08:42:04 +04:00
converted = im.convert('P')
comparable = converted.convert('RGBA')
assert_image_similar(im, comparable, 20)
2014-05-17 20:08:08 +04:00
2014-05-18 00:33:50 +04:00
2014-05-17 20:08:08 +04:00
def test_trns_p():
2014-03-26 10:33:49 +04:00
im = lena('P')
2014-05-18 00:33:50 +04:00
im.info['transparency'] = 0
2014-03-26 10:33:49 +04:00
f = tempfile('temp.png')
l = im.convert('L')
2014-05-18 00:33:50 +04:00
assert_equal(l.info['transparency'], 0) # undone
2014-03-26 10:33:49 +04:00
assert_no_exception(lambda: l.save(f))
rgb = im.convert('RGB')
2014-05-18 00:33:50 +04:00
assert_equal(rgb.info['transparency'], (0, 0, 0)) # undone
2014-03-26 10:33:49 +04:00
assert_no_exception(lambda: rgb.save(f))
2014-05-17 20:08:08 +04:00
# ref https://github.com/python-pillow/Pillow/issues/664
2014-05-18 00:33:50 +04:00
2014-05-17 20:08:08 +04:00
def test_trns_p_rgba():
# Arrange
im = lena('P')
im.info['transparency'] = 128
# Act
rgba = im.convert('RGBA')
# Assert
assert_false('transparency' in rgba.info)
2014-03-26 10:33:49 +04:00
def test_trns_l():
im = lena('L')
im.info['transparency'] = 128
f = tempfile('temp.png')
2014-05-17 20:08:08 +04:00
2014-03-26 10:33:49 +04:00
rgb = im.convert('RGB')
2014-05-18 00:33:50 +04:00
assert_equal(rgb.info['transparency'], (128, 128, 128)) # undone
2014-03-26 10:33:49 +04:00
assert_no_exception(lambda: rgb.save(f))
2014-03-26 10:33:49 +04:00
p = im.convert('P')
assert_true('transparency' in p.info)
assert_no_exception(lambda: p.save(f))
2014-03-30 20:26:03 +04:00
p = assert_warning(UserWarning,
2014-05-18 00:33:50 +04:00
lambda: im.convert('P', palette=Image.ADAPTIVE))
assert_false('transparency' in p.info)
assert_no_exception(lambda: p.save(f))
2014-05-17 20:08:08 +04:00
2014-03-26 10:33:49 +04:00
def test_trns_RGB():
im = lena('RGB')
2014-05-18 00:33:50 +04:00
im.info['transparency'] = im.getpixel((0, 0))
2014-03-26 10:33:49 +04:00
f = tempfile('temp.png')
2014-05-17 20:08:08 +04:00
2014-03-26 10:33:49 +04:00
l = im.convert('L')
2014-05-18 00:33:50 +04:00
assert_equal(l.info['transparency'], l.getpixel((0, 0))) # undone
2014-03-26 10:33:49 +04:00
assert_no_exception(lambda: l.save(f))
2014-03-26 10:33:49 +04:00
p = im.convert('P')
assert_true('transparency' in p.info)
assert_no_exception(lambda: p.save(f))
2014-05-17 20:08:08 +04:00
2014-03-30 20:26:03 +04:00
p = assert_warning(UserWarning,
2014-05-18 00:33:50 +04:00
lambda: im.convert('P', palette=Image.ADAPTIVE))
assert_false('transparency' in p.info)
assert_no_exception(lambda: p.save(f))