mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
Added transparency to matrix conversion
This commit is contained in:
parent
1e56ed8c00
commit
e98469ecf6
|
@ -187,6 +187,7 @@ class TestImageConvert(PillowTestCase):
|
||||||
def matrix_convert(mode):
|
def matrix_convert(mode):
|
||||||
# Arrange
|
# Arrange
|
||||||
im = hopper('RGB')
|
im = hopper('RGB')
|
||||||
|
im.info['transparency'] = (255, 0, 0)
|
||||||
matrix = (
|
matrix = (
|
||||||
0.412453, 0.357580, 0.180423, 0,
|
0.412453, 0.357580, 0.180423, 0,
|
||||||
0.212671, 0.715160, 0.072169, 0,
|
0.212671, 0.715160, 0.072169, 0,
|
||||||
|
@ -203,9 +204,11 @@ class TestImageConvert(PillowTestCase):
|
||||||
target = Image.open('Tests/images/hopper-XYZ.png')
|
target = Image.open('Tests/images/hopper-XYZ.png')
|
||||||
if converted_im.mode == 'RGB':
|
if converted_im.mode == 'RGB':
|
||||||
self.assert_image_similar(converted_im, target, 3)
|
self.assert_image_similar(converted_im, target, 3)
|
||||||
|
self.assertEqual(converted_im.info['transparency'], (105, 54, 4))
|
||||||
else:
|
else:
|
||||||
self.assert_image_similar(converted_im,
|
self.assert_image_similar(converted_im,
|
||||||
target.getchannel(0), 1)
|
target.getchannel(0), 1)
|
||||||
|
self.assertEqual(converted_im.info['transparency'], 105)
|
||||||
|
|
||||||
matrix_convert('RGB')
|
matrix_convert('RGB')
|
||||||
matrix_convert('L')
|
matrix_convert('L')
|
||||||
|
|
|
@ -900,12 +900,28 @@ class Image(object):
|
||||||
if not mode or (mode == self.mode and not matrix):
|
if not mode or (mode == self.mode and not matrix):
|
||||||
return self.copy()
|
return self.copy()
|
||||||
|
|
||||||
|
has_transparency = self.info.get('transparency') is not None
|
||||||
if matrix:
|
if matrix:
|
||||||
# matrix conversion
|
# matrix conversion
|
||||||
if mode not in ("L", "RGB"):
|
if mode not in ("L", "RGB"):
|
||||||
raise ValueError("illegal conversion")
|
raise ValueError("illegal conversion")
|
||||||
im = self.im.convert_matrix(mode, matrix)
|
im = self.im.convert_matrix(mode, matrix)
|
||||||
return self._new(im)
|
new = self._new(im)
|
||||||
|
if has_transparency and self.im.bands == 3:
|
||||||
|
transparency = new.info['transparency']
|
||||||
|
|
||||||
|
def convert_transparency(m, v):
|
||||||
|
v = m[0]*v[0] + m[1]*v[1] + m[2]*v[2] + m[3]*0.5
|
||||||
|
return max(0, min(255, int(v)))
|
||||||
|
if mode == "L":
|
||||||
|
transparency = convert_transparency(matrix, transparency)
|
||||||
|
elif len(mode) == 3:
|
||||||
|
transparency = tuple([
|
||||||
|
convert_transparency(matrix[i*4:i*4+4], transparency)
|
||||||
|
for i in range(0, len(transparency))
|
||||||
|
])
|
||||||
|
new.info['transparency'] = transparency
|
||||||
|
return new
|
||||||
|
|
||||||
if mode == "P" and self.mode == "RGBA":
|
if mode == "P" and self.mode == "RGBA":
|
||||||
return self.quantize(colors)
|
return self.quantize(colors)
|
||||||
|
@ -913,8 +929,7 @@ class Image(object):
|
||||||
trns = None
|
trns = None
|
||||||
delete_trns = False
|
delete_trns = False
|
||||||
# transparency handling
|
# transparency handling
|
||||||
if "transparency" in self.info and \
|
if has_transparency:
|
||||||
self.info['transparency'] is not None:
|
|
||||||
if self.mode in ('L', 'RGB') and mode == 'RGBA':
|
if self.mode in ('L', 'RGB') and mode == 'RGBA':
|
||||||
# Use transparent conversion to promote from transparent
|
# Use transparent conversion to promote from transparent
|
||||||
# color to an alpha channel.
|
# color to an alpha channel.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user