2019-02-03 18:34:53 +03:00
|
|
|
from .helper import PillowTestCase, hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from PIL import ImageEnhance
|
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImageEnhance(PillowTestCase):
|
|
|
|
def test_sanity(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# FIXME: assert_image
|
|
|
|
# Implicit asserts no exception:
|
2014-09-18 19:46:22 +04:00
|
|
|
ImageEnhance.Color(hopper()).enhance(0.5)
|
|
|
|
ImageEnhance.Contrast(hopper()).enhance(0.5)
|
|
|
|
ImageEnhance.Brightness(hopper()).enhance(0.5)
|
|
|
|
ImageEnhance.Sharpness(hopper()).enhance(0.5)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_crash(self):
|
|
|
|
|
|
|
|
# crashes on small images
|
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
ImageEnhance.Sharpness(im).enhance(0.5)
|
|
|
|
|
2014-09-18 19:49:13 +04:00
|
|
|
def _half_transparent_image(self):
|
|
|
|
# returns an image, half transparent, half solid
|
2019-06-13 18:54:46 +03:00
|
|
|
im = hopper("RGB")
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
transparent = Image.new("L", im.size, 0)
|
|
|
|
solid = Image.new("L", (im.size[0] // 2, im.size[1]), 255)
|
2015-04-24 02:26:52 +03:00
|
|
|
transparent.paste(solid, (0, 0))
|
2014-09-18 19:49:13 +04:00
|
|
|
im.putalpha(transparent)
|
|
|
|
|
|
|
|
return im
|
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
def _check_alpha(self, im, original, op, amount):
|
2014-09-18 19:49:13 +04:00
|
|
|
self.assertEqual(im.getbands(), original.getbands())
|
2019-06-13 18:54:46 +03:00
|
|
|
self.assert_image_equal(
|
|
|
|
im.getchannel("A"),
|
|
|
|
original.getchannel("A"),
|
|
|
|
"Diff on %s: %s" % (op, amount),
|
|
|
|
)
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2014-09-18 19:49:13 +04:00
|
|
|
def test_alpha(self):
|
|
|
|
# Issue https://github.com/python-pillow/Pillow/issues/899
|
|
|
|
# Is alpha preserved through image enhancement?
|
|
|
|
|
|
|
|
original = self._half_transparent_image()
|
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
for op in ["Color", "Brightness", "Contrast", "Sharpness"]:
|
2015-04-24 02:26:52 +03:00
|
|
|
for amount in [0, 0.5, 1.0]:
|
2018-06-24 15:32:25 +03:00
|
|
|
self._check_alpha(
|
|
|
|
getattr(ImageEnhance, op)(original).enhance(amount),
|
2019-06-13 18:54:46 +03:00
|
|
|
original,
|
|
|
|
op,
|
|
|
|
amount,
|
|
|
|
)
|