Merge pull request #1 from wiredfool/p_rgba_transparency

Tweak for #664
This commit is contained in:
Hugo 2014-05-20 11:20:07 +03:00
commit f8c5812154
2 changed files with 60 additions and 41 deletions

View File

@ -802,7 +802,8 @@ class Image:
# after quantization. # after quantization.
trns_im = trns_im.convert('RGB') trns_im = trns_im.convert('RGB')
trns = trns_im.getpixel((0,0)) trns = trns_im.getpixel((0,0))
elif self.mode == 'P' and mode == 'RGBA':
delete_trns = True
if mode == "P" and palette == ADAPTIVE: if mode == "P" and palette == ADAPTIVE:
im = self.im.quantize(colors) im = self.im.quantize(colors)

View File

@ -2,6 +2,7 @@ from tester import *
from PIL import Image from PIL import Image
def test_sanity(): def test_sanity():
def convert(im, mode): def convert(im, mode):
@ -16,6 +17,7 @@ def test_sanity():
for mode in modes: for mode in modes:
yield_test(convert, im, mode) yield_test(convert, im, mode)
def test_default(): def test_default():
im = lena("P") im = lena("P")
@ -26,26 +28,29 @@ def test_default():
assert_image(im, "RGB", im.size) assert_image(im, "RGB", im.size)
# ref https://github.com/python-imaging/Pillow/issues/274 # ref https://github.com/python-imaging/Pillow/issues/274
def _test_float_conversion(im): def _test_float_conversion(im):
orig = im.getpixel((5,5)) orig = im.getpixel((5, 5))
converted = im.convert('F').getpixel((5,5)) converted = im.convert('F').getpixel((5, 5))
assert_equal(orig, converted) assert_equal(orig, converted)
def test_8bit(): def test_8bit():
im = Image.open('Images/lena.jpg') im = Image.open('Images/lena.jpg')
_test_float_conversion(im.convert('L')) _test_float_conversion(im.convert('L'))
def test_16bit(): def test_16bit():
im = Image.open('Tests/images/16bit.cropped.tif') im = Image.open('Tests/images/16bit.cropped.tif')
_test_float_conversion(im) _test_float_conversion(im)
def test_16bit_workaround(): def test_16bit_workaround():
im = Image.open('Tests/images/16bit.cropped.tif') im = Image.open('Tests/images/16bit.cropped.tif')
_test_float_conversion(im.convert('I')) _test_float_conversion(im.convert('I'))
def test_rgba_p(): def test_rgba_p():
im = lena('RGBA') im = lena('RGBA')
im.putalpha(lena('L')) im.putalpha(lena('L'))
@ -55,21 +60,36 @@ def test_rgba_p():
assert_image_similar(im, comparable, 20) assert_image_similar(im, comparable, 20)
def test_trns_p(): def test_trns_p():
im = lena('P') im = lena('P')
im.info['transparency']=0 im.info['transparency'] = 0
f = tempfile('temp.png') f = tempfile('temp.png')
l = im.convert('L') l = im.convert('L')
assert_equal(l.info['transparency'], 0) # undone assert_equal(l.info['transparency'], 0) # undone
assert_no_exception(lambda: l.save(f)) assert_no_exception(lambda: l.save(f))
rgb = im.convert('RGB') rgb = im.convert('RGB')
assert_equal(rgb.info['transparency'], (0,0,0)) # undone assert_equal(rgb.info['transparency'], (0, 0, 0)) # undone
assert_no_exception(lambda: rgb.save(f)) assert_no_exception(lambda: rgb.save(f))
# ref https://github.com/python-imaging/Pillow/issues/664
def test_trns_p_rgba():
# Arrange
im = lena('P')
im.info['transparency'] = 128
# Act
rgba = im.convert('RGBA')
# Assert
assert_false('transparency' in rgba.info)
def test_trns_l(): def test_trns_l():
im = lena('L') im = lena('L')
im.info['transparency'] = 128 im.info['transparency'] = 128
@ -77,7 +97,7 @@ def test_trns_l():
f = tempfile('temp.png') f = tempfile('temp.png')
rgb = im.convert('RGB') rgb = im.convert('RGB')
assert_equal(rgb.info['transparency'], (128,128,128)) # undone assert_equal(rgb.info['transparency'], (128, 128, 128)) # undone
assert_no_exception(lambda: rgb.save(f)) assert_no_exception(lambda: rgb.save(f))
p = im.convert('P') p = im.convert('P')
@ -85,19 +105,19 @@ def test_trns_l():
assert_no_exception(lambda: p.save(f)) assert_no_exception(lambda: p.save(f))
p = assert_warning(UserWarning, p = assert_warning(UserWarning,
lambda: im.convert('P', palette = Image.ADAPTIVE)) lambda: im.convert('P', palette=Image.ADAPTIVE))
assert_false('transparency' in p.info) assert_false('transparency' in p.info)
assert_no_exception(lambda: p.save(f)) assert_no_exception(lambda: p.save(f))
def test_trns_RGB(): def test_trns_RGB():
im = lena('RGB') im = lena('RGB')
im.info['transparency'] = im.getpixel((0,0)) im.info['transparency'] = im.getpixel((0, 0))
f = tempfile('temp.png') f = tempfile('temp.png')
l = im.convert('L') l = im.convert('L')
assert_equal(l.info['transparency'], l.getpixel((0,0))) # undone assert_equal(l.info['transparency'], l.getpixel((0, 0))) # undone
assert_no_exception(lambda: l.save(f)) assert_no_exception(lambda: l.save(f))
p = im.convert('P') p = im.convert('P')
@ -105,8 +125,6 @@ def test_trns_RGB():
assert_no_exception(lambda: p.save(f)) assert_no_exception(lambda: p.save(f))
p = assert_warning(UserWarning, p = assert_warning(UserWarning,
lambda: im.convert('P', palette = Image.ADAPTIVE)) lambda: im.convert('P', palette=Image.ADAPTIVE))
assert_false('transparency' in p.info) assert_false('transparency' in p.info)
assert_no_exception(lambda: p.save(f)) assert_no_exception(lambda: p.save(f))