mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 18:26:17 +03:00
Merge pull request #909 from wiredfool/imageenhance
Retain alpha in ImageEnhance operations
This commit is contained in:
commit
0b82dacea2
|
@ -47,8 +47,11 @@ class Color(_Enhance):
|
|||
"""
|
||||
def __init__(self, image):
|
||||
self.image = image
|
||||
self.degenerate = image.convert("L").convert(image.mode)
|
||||
self.intermediate_mode = 'L'
|
||||
if 'A' in image.getbands():
|
||||
self.intermediate_mode = 'LA'
|
||||
|
||||
self.degenerate = image.convert(self.intermediate_mode).convert(image.mode)
|
||||
|
||||
class Contrast(_Enhance):
|
||||
"""Adjust image contrast.
|
||||
|
@ -62,6 +65,9 @@ class Contrast(_Enhance):
|
|||
mean = int(ImageStat.Stat(image.convert("L")).mean[0] + 0.5)
|
||||
self.degenerate = Image.new("L", image.size, mean).convert(image.mode)
|
||||
|
||||
if 'A' in image.getbands():
|
||||
self.degenerate.putalpha(image.split()[-1])
|
||||
|
||||
|
||||
class Brightness(_Enhance):
|
||||
"""Adjust image brightness.
|
||||
|
@ -74,6 +80,9 @@ class Brightness(_Enhance):
|
|||
self.image = image
|
||||
self.degenerate = Image.new(image.mode, image.size, 0)
|
||||
|
||||
if 'A' in image.getbands():
|
||||
self.degenerate.putalpha(image.split()[-1])
|
||||
|
||||
|
||||
class Sharpness(_Enhance):
|
||||
"""Adjust image sharpness.
|
||||
|
@ -85,3 +94,6 @@ class Sharpness(_Enhance):
|
|||
def __init__(self, image):
|
||||
self.image = image
|
||||
self.degenerate = image.filter(ImageFilter.SMOOTH)
|
||||
|
||||
if 'A' in image.getbands():
|
||||
self.degenerate.putalpha(image.split()[-1])
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from helper import unittest, PillowTestCase, lena
|
||||
from helper import unittest, PillowTestCase, hopper
|
||||
|
||||
from PIL import Image
|
||||
from PIL import ImageEnhance
|
||||
|
@ -10,10 +10,10 @@ class TestImageEnhance(PillowTestCase):
|
|||
|
||||
# FIXME: assert_image
|
||||
# Implicit asserts no exception:
|
||||
ImageEnhance.Color(lena()).enhance(0.5)
|
||||
ImageEnhance.Contrast(lena()).enhance(0.5)
|
||||
ImageEnhance.Brightness(lena()).enhance(0.5)
|
||||
ImageEnhance.Sharpness(lena()).enhance(0.5)
|
||||
ImageEnhance.Color(hopper()).enhance(0.5)
|
||||
ImageEnhance.Contrast(hopper()).enhance(0.5)
|
||||
ImageEnhance.Brightness(hopper()).enhance(0.5)
|
||||
ImageEnhance.Sharpness(hopper()).enhance(0.5)
|
||||
|
||||
def test_crash(self):
|
||||
|
||||
|
@ -22,6 +22,34 @@ class TestImageEnhance(PillowTestCase):
|
|||
ImageEnhance.Sharpness(im).enhance(0.5)
|
||||
|
||||
|
||||
def _half_transparent_image(self):
|
||||
# returns an image, half transparent, half solid
|
||||
im = hopper('RGB')
|
||||
|
||||
transparent = Image.new('L', im.size, 0)
|
||||
solid = Image.new('L', (im.size[0]//2, im.size[1]), 255)
|
||||
transparent.paste(solid, (0,0))
|
||||
im.putalpha(transparent)
|
||||
|
||||
return im
|
||||
|
||||
def _check_alpha(self,im, original, op, amount):
|
||||
self.assertEqual(im.getbands(), original.getbands())
|
||||
self.assert_image_equal(im.split()[-1], original.split()[-1],
|
||||
"Diff on %s: %s" % (op, amount))
|
||||
|
||||
def test_alpha(self):
|
||||
# Issue https://github.com/python-pillow/Pillow/issues/899
|
||||
# Is alpha preserved through image enhancement?
|
||||
|
||||
original = self._half_transparent_image()
|
||||
|
||||
for op in ['Color', 'Brightness', 'Contrast', 'Sharpness']:
|
||||
for amount in [0,0.5,1.0]:
|
||||
self._check_alpha(getattr(ImageEnhance,op)(original).enhance(amount),
|
||||
original, op, amount)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user