Pillow/Tests/test_image_convert.py

140 lines
3.6 KiB
Python
Raw Normal View History

from helper import unittest, PillowTestCase, hopper
from PIL import Image
2014-05-18 00:33:50 +04:00
2014-06-10 13:10:47 +04:00
class TestImageConvert(PillowTestCase):
2014-06-10 13:10:47 +04:00
def test_sanity(self):
2014-06-10 13:10:47 +04:00
def convert(im, mode):
out = im.convert(mode)
self.assertEqual(out.mode, mode)
self.assertEqual(out.size, im.size)
2014-06-10 13:10:47 +04:00
modes = "1", "L", "I", "F", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr"
2014-06-10 13:10:47 +04:00
for mode in modes:
im = hopper(mode)
2014-06-10 13:10:47 +04:00
for mode in modes:
convert(im, mode)
2014-05-18 00:33:50 +04:00
2014-06-10 13:10:47 +04:00
def test_default(self):
im = hopper("P")
2014-06-10 13:10:47 +04:00
self.assert_image(im, "P", im.size)
im = im.convert()
self.assert_image(im, "RGB", im.size)
im = im.convert()
self.assert_image(im, "RGB", im.size)
2014-05-18 00:33:50 +04:00
2014-06-10 13:10:47 +04:00
# ref https://github.com/python-pillow/Pillow/issues/274
2014-06-10 13:10:47 +04:00
def _test_float_conversion(self, im):
orig = im.getpixel((5, 5))
converted = im.convert('F').getpixel((5, 5))
self.assertEqual(orig, converted)
2014-05-18 00:33:50 +04:00
2014-06-10 13:10:47 +04:00
def test_8bit(self):
im = Image.open('Tests/images/hopper.jpg')
2014-06-10 13:10:47 +04:00
self._test_float_conversion(im.convert('L'))
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
def test_16bit(self):
im = Image.open('Tests/images/16bit.cropped.tif')
self._test_float_conversion(im)
2014-05-18 00:33:50 +04:00
2014-06-10 13:10:47 +04:00
def test_16bit_workaround(self):
im = Image.open('Tests/images/16bit.cropped.tif')
self._test_float_conversion(im.convert('I'))
2014-06-10 13:10:47 +04:00
def test_rgba_p(self):
im = hopper('RGBA')
im.putalpha(hopper('L'))
2014-03-26 08:42:04 +04:00
2014-06-10 13:10:47 +04:00
converted = im.convert('P')
comparable = converted.convert('RGBA')
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
self.assert_image_similar(im, comparable, 20)
2014-05-18 00:33:50 +04:00
2014-06-10 13:10:47 +04:00
def test_trns_p(self):
im = hopper('P')
2014-06-10 13:10:47 +04:00
im.info['transparency'] = 0
2014-03-26 10:33:49 +04:00
2014-06-10 13:10:47 +04:00
f = self.tempfile('temp.png')
2014-03-26 10:33:49 +04:00
2014-06-10 13:10:47 +04:00
l = im.convert('L')
self.assertEqual(l.info['transparency'], 0) # undone
l.save(f)
2014-03-26 10:33:49 +04:00
2014-06-10 13:10:47 +04:00
rgb = im.convert('RGB')
self.assertEqual(rgb.info['transparency'], (0, 0, 0)) # undone
rgb.save(f)
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
# ref https://github.com/python-pillow/Pillow/issues/664
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
def test_trns_p_rgba(self):
# Arrange
im = hopper('P')
2014-06-10 13:10:47 +04:00
im.info['transparency'] = 128
2014-05-18 00:33:50 +04:00
2014-06-10 13:10:47 +04:00
# Act
rgba = im.convert('RGBA')
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
# Assert
self.assertNotIn('transparency', rgba.info)
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
def test_trns_l(self):
im = hopper('L')
2014-06-10 13:10:47 +04:00
im.info['transparency'] = 128
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
f = self.tempfile('temp.png')
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
rgb = im.convert('RGB')
self.assertEqual(rgb.info['transparency'], (128, 128, 128)) # undone
rgb.save(f)
2014-03-26 10:33:49 +04:00
2014-06-10 13:10:47 +04:00
p = im.convert('P')
self.assertIn('transparency', p.info)
p.save(f)
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
p = self.assert_warning(
UserWarning,
lambda: im.convert('P', palette=Image.ADAPTIVE))
self.assertNotIn('transparency', p.info)
p.save(f)
2014-06-10 13:10:47 +04:00
def test_trns_RGB(self):
im = hopper('RGB')
2014-06-10 13:10:47 +04:00
im.info['transparency'] = im.getpixel((0, 0))
2014-06-10 13:10:47 +04:00
f = self.tempfile('temp.png')
2014-06-10 13:10:47 +04:00
l = im.convert('L')
self.assertEqual(l.info['transparency'], l.getpixel((0, 0))) # undone
l.save(f)
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
p = im.convert('P')
self.assertIn('transparency', p.info)
p.save(f)
2014-03-26 10:33:49 +04:00
2014-06-10 13:10:47 +04:00
p = self.assert_warning(
UserWarning,
lambda: im.convert('P', palette=Image.ADAPTIVE))
self.assertNotIn('transparency', p.info)
p.save(f)
2014-05-17 20:08:08 +04:00
2016-06-26 13:50:38 +03:00
def test_p_la(self):
im = hopper('RGBA')
alpha = hopper('L')
im.putalpha(alpha)
comparable = im.convert('P').convert('LA').split()[1]
self.assert_image_similar(alpha, comparable, 5)
2016-06-26 13:50:38 +03:00
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()
2014-05-17 20:08:08 +04:00
2014-06-10 13:10:47 +04:00
# End of file