This commit is contained in:
wiredfool 2014-03-26 04:46:16 +00:00
commit 7813748f7a
4 changed files with 32 additions and 2 deletions

View File

@ -4,6 +4,9 @@ Changelog (Pillow)
2.4.0 (unreleased)
------------------
- Conversions enabled from RGBA->P, Fixes #544
[wiredfool]
- Add more detailed error messages to Image.py
[larsmans]

View File

@ -735,6 +735,9 @@ class Image:
im = self.im.convert_matrix(mode, matrix)
return self._new(im)
if mode == "P" and self.mode == "RGBA":
return self.quantize(colors)
if mode == "P" and palette == ADAPTIVE:
im = self.im.quantize(colors)
new = self._new(im)
@ -762,7 +765,7 @@ class Image:
return self._new(im)
def quantize(self, colors=256, method=0, kmeans=0, palette=None):
def quantize(self, colors=256, method=None, kmeans=0, palette=None):
# methods:
# 0 = median cut
@ -774,6 +777,17 @@ class Image:
self.load()
if method is None:
# defaults:
method = 0
if self.mode == 'RGBA':
method = 2
if self.mode == 'RGBA' and method != 2:
# Caller specified an invalid mode.
raise ValueError('Fast Octree (method == 2) is the ' +
' only valid method for quantizing RGBA images')
if palette:
# use palette from reference image
palette.load()

View File

@ -46,5 +46,13 @@ def test_16bit_workaround():
im = Image.open('Tests/images/16bit.cropped.tif')
_test_float_conversion(im.convert('I'))
def test_rgba_p():
im = lena('RGBA')
im.putalpha(lena('L'))
converted = im.convert('P')
comparable = converted.convert('RGBA')
assert_image_similar(im, comparable, 20)

View File

@ -20,3 +20,8 @@ def test_octree_quantize():
assert_image(im, "P", im.size)
assert len(im.getcolors()) == 100
def test_rgba_quantize():
im = lena('RGBA')
assert_no_exception(lambda: im.quantize())
assert_exception(Exception, lambda: im.quantize(method=0))