2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from .helper import PillowTestCase, hopper
|
|
|
|
|
2014-05-18 00:33:50 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImageConvert(PillowTestCase):
|
|
|
|
def test_sanity(self):
|
|
|
|
def convert(im, mode):
|
|
|
|
out = im.convert(mode)
|
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, im.size)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
modes = (
|
|
|
|
"1",
|
|
|
|
"L",
|
|
|
|
"LA",
|
|
|
|
"P",
|
|
|
|
"PA",
|
|
|
|
"I",
|
|
|
|
"F",
|
|
|
|
"RGB",
|
|
|
|
"RGBA",
|
|
|
|
"RGBX",
|
|
|
|
"CMYK",
|
|
|
|
"YCbCr",
|
2019-08-02 14:35:17 +03:00
|
|
|
"HSV",
|
2019-06-13 18:54:24 +03:00
|
|
|
)
|
2013-07-09 10:21:41 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
for mode in modes:
|
2014-09-05 14:03:56 +04:00
|
|
|
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
|
|
|
|
2016-12-27 15:53:23 +03:00
|
|
|
# Check 0
|
2017-01-29 15:03:38 +03:00
|
|
|
im = Image.new(mode, (0, 0))
|
2016-12-27 15:53:23 +03:00
|
|
|
for mode in modes:
|
|
|
|
convert(im, mode)
|
2017-01-29 15:03:38 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_default(self):
|
2013-07-09 10:21:41 +04:00
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
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
|
2013-07-09 10:21:41 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def _test_float_conversion(self, im):
|
|
|
|
orig = im.getpixel((5, 5))
|
2019-06-13 18:54:24 +03:00
|
|
|
converted = im.convert("F").getpixel((5, 5))
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(orig, converted)
|
2014-05-18 00:33:50 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_8bit(self):
|
2019-06-13 18:54:24 +03:00
|
|
|
im = Image.open("Tests/images/hopper.jpg")
|
|
|
|
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):
|
2019-06-13 18:54:24 +03:00
|
|
|
im = Image.open("Tests/images/16bit.cropped.tif")
|
2014-06-10 13:10:47 +04:00
|
|
|
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):
|
2019-06-13 18:54:24 +03:00
|
|
|
im = Image.open("Tests/images/16bit.cropped.tif")
|
|
|
|
self._test_float_conversion(im.convert("I"))
|
2013-07-09 10:21:41 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_rgba_p(self):
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGBA")
|
|
|
|
im.putalpha(hopper("L"))
|
2014-03-26 08:42:04 +04:00
|
|
|
|
2019-06-13 18:54:24 +03: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):
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("P")
|
|
|
|
im.info["transparency"] = 0
|
2014-03-26 10:33:49 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
f = self.tempfile("temp.png")
|
2014-03-26 10:33:49 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_l = im.convert("L")
|
|
|
|
self.assertEqual(im_l.info["transparency"], 0) # undone
|
2018-06-15 16:44:22 +03:00
|
|
|
im_l.save(f)
|
2014-03-26 10:33:49 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_rgb = im.convert("RGB")
|
|
|
|
self.assertEqual(im_rgb.info["transparency"], (0, 0, 0)) # undone
|
2018-06-15 16:44:22 +03:00
|
|
|
im_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
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("P")
|
|
|
|
im.info["transparency"] = 128
|
2014-05-18 00:33:50 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
2019-06-13 18:54:24 +03:00
|
|
|
im_rgba = im.convert("RGBA")
|
2014-05-17 20:08:08 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assertNotIn("transparency", im_rgba.info)
|
2017-08-31 16:18:59 +03:00
|
|
|
# https://github.com/python-pillow/Pillow/issues/2702
|
2018-08-04 21:08:40 +03:00
|
|
|
self.assertIsNone(im_rgba.palette)
|
2018-01-27 09:07:24 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_trns_l(self):
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("L")
|
|
|
|
im.info["transparency"] = 128
|
2014-05-17 20:08:08 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
f = self.tempfile("temp.png")
|
2014-05-17 20:08:08 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_rgb = im.convert("RGB")
|
|
|
|
self.assertEqual(im_rgb.info["transparency"], (128, 128, 128)) # undone
|
2018-06-15 16:44:22 +03:00
|
|
|
im_rgb.save(f)
|
2014-03-26 10:33:49 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_p = im.convert("P")
|
|
|
|
self.assertIn("transparency", im_p.info)
|
2018-06-15 16:44:22 +03:00
|
|
|
im_p.save(f)
|
2014-05-17 20:08:08 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_p = self.assert_warning(UserWarning, im.convert, "P", palette=Image.ADAPTIVE)
|
|
|
|
self.assertNotIn("transparency", im_p.info)
|
2018-06-15 16:44:22 +03:00
|
|
|
im_p.save(f)
|
2014-03-26 11:01:10 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_trns_RGB(self):
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGB")
|
|
|
|
im.info["transparency"] = im.getpixel((0, 0))
|
2014-03-26 11:01:10 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
f = self.tempfile("temp.png")
|
2014-03-26 11:01:10 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_l = im.convert("L")
|
|
|
|
self.assertEqual(im_l.info["transparency"], im_l.getpixel((0, 0))) # undone
|
2018-06-15 16:44:22 +03:00
|
|
|
im_l.save(f)
|
2014-05-17 20:08:08 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_p = im.convert("P")
|
|
|
|
self.assertIn("transparency", im_p.info)
|
2018-06-15 16:44:22 +03:00
|
|
|
im_p.save(f)
|
2014-03-26 10:33:49 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_rgba = im.convert("RGBA")
|
|
|
|
self.assertNotIn("transparency", im_rgba.info)
|
2018-06-15 16:44:22 +03:00
|
|
|
im_rgba.save(f)
|
2017-07-18 16:00:09 +03:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im_p = self.assert_warning(UserWarning, im.convert, "P", palette=Image.ADAPTIVE)
|
|
|
|
self.assertNotIn("transparency", im_p.info)
|
2018-06-15 16:44:22 +03:00
|
|
|
im_p.save(f)
|
2014-05-17 20:08:08 +04:00
|
|
|
|
2018-07-14 11:55:13 +03:00
|
|
|
def test_gif_with_rgba_palette_to_p(self):
|
|
|
|
# See https://github.com/python-pillow/Pillow/issues/2433
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open("Tests/images/hopper.gif") as im:
|
|
|
|
im.info["transparency"] = 255
|
|
|
|
im.load()
|
|
|
|
self.assertEqual(im.palette.mode, "RGBA")
|
|
|
|
im_p = im.convert("P")
|
2018-07-14 11:55:13 +03:00
|
|
|
|
|
|
|
# Should not raise ValueError: unrecognized raw mode
|
|
|
|
im_p.load()
|
|
|
|
|
2016-06-26 13:50:38 +03:00
|
|
|
def test_p_la(self):
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGBA")
|
|
|
|
alpha = hopper("L")
|
2016-06-30 16:59:04 +03:00
|
|
|
im.putalpha(alpha)
|
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
comparable = im.convert("P").convert("LA").getchannel("A")
|
2016-06-30 16:59:04 +03:00
|
|
|
|
|
|
|
self.assert_image_similar(alpha, comparable, 5)
|
2016-06-26 13:50:38 +03:00
|
|
|
|
2017-01-29 15:03:38 +03:00
|
|
|
def test_matrix_illegal_conversion(self):
|
|
|
|
# Arrange
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("CMYK")
|
|
|
|
# fmt: off
|
2017-01-29 15:03:38 +03:00
|
|
|
matrix = (
|
|
|
|
0.412453, 0.357580, 0.180423, 0,
|
|
|
|
0.212671, 0.715160, 0.072169, 0,
|
|
|
|
0.019334, 0.119193, 0.950227, 0)
|
2019-06-13 18:54:24 +03:00
|
|
|
# fmt: on
|
|
|
|
self.assertNotEqual(im.mode, "RGB")
|
2017-01-29 15:03:38 +03:00
|
|
|
|
|
|
|
# Act / Assert
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assertRaises(ValueError, im.convert, mode="CMYK", matrix=matrix)
|
2017-01-29 15:03:38 +03:00
|
|
|
|
2017-01-29 16:55:17 +03:00
|
|
|
def test_matrix_wrong_mode(self):
|
|
|
|
# Arrange
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("L")
|
|
|
|
# fmt: off
|
2017-01-29 16:55:17 +03:00
|
|
|
matrix = (
|
|
|
|
0.412453, 0.357580, 0.180423, 0,
|
|
|
|
0.212671, 0.715160, 0.072169, 0,
|
|
|
|
0.019334, 0.119193, 0.950227, 0)
|
2019-06-13 18:54:24 +03:00
|
|
|
# fmt: on
|
|
|
|
self.assertEqual(im.mode, "L")
|
2017-01-29 16:55:17 +03:00
|
|
|
|
|
|
|
# Act / Assert
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assertRaises(ValueError, im.convert, mode="L", matrix=matrix)
|
2017-01-29 16:55:17 +03:00
|
|
|
|
2017-01-30 15:27:29 +03:00
|
|
|
def test_matrix_xyz(self):
|
2017-01-29 15:24:49 +03:00
|
|
|
def matrix_convert(mode):
|
|
|
|
# Arrange
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGB")
|
|
|
|
im.info["transparency"] = (255, 0, 0)
|
|
|
|
# fmt: off
|
2017-01-29 15:24:49 +03:00
|
|
|
matrix = (
|
|
|
|
0.412453, 0.357580, 0.180423, 0,
|
|
|
|
0.212671, 0.715160, 0.072169, 0,
|
|
|
|
0.019334, 0.119193, 0.950227, 0)
|
2019-06-13 18:54:24 +03:00
|
|
|
# fmt: on
|
|
|
|
self.assertEqual(im.mode, "RGB")
|
2017-01-29 15:24:49 +03:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# Convert an RGB image to the CIE XYZ colour space
|
|
|
|
converted_im = im.convert(mode=mode, matrix=matrix)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
self.assertEqual(converted_im.mode, mode)
|
|
|
|
self.assertEqual(converted_im.size, im.size)
|
2019-06-13 18:54:24 +03:00
|
|
|
target = Image.open("Tests/images/hopper-XYZ.png")
|
|
|
|
if converted_im.mode == "RGB":
|
2017-02-21 00:41:28 +03:00
|
|
|
self.assert_image_similar(converted_im, target, 3)
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assertEqual(converted_im.info["transparency"], (105, 54, 4))
|
2017-02-21 00:41:28 +03:00
|
|
|
else:
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assert_image_similar(converted_im, target.getchannel(0), 1)
|
|
|
|
self.assertEqual(converted_im.info["transparency"], 105)
|
2017-02-21 00:41:28 +03:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
matrix_convert("RGB")
|
|
|
|
matrix_convert("L")
|
2017-01-29 15:03:38 +03:00
|
|
|
|
2017-01-30 15:28:00 +03:00
|
|
|
def test_matrix_identity(self):
|
|
|
|
# Arrange
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGB")
|
|
|
|
# fmt: off
|
2017-01-30 15:28:00 +03:00
|
|
|
identity_matrix = (
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0)
|
2019-06-13 18:54:24 +03:00
|
|
|
# fmt: on
|
|
|
|
self.assertEqual(im.mode, "RGB")
|
2017-01-30 15:28:00 +03:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# Convert with an identity matrix
|
2019-06-13 18:54:24 +03:00
|
|
|
converted_im = im.convert(mode="RGB", matrix=identity_matrix)
|
2017-01-30 15:28:00 +03:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
# No change
|
|
|
|
self.assert_image_equal(converted_im, im)
|