This commit is contained in:
Kovid Goyal 2016-05-09 19:36:13 +05:30
parent d03f4490f3
commit 9dc6fbc6b9

View File

@ -277,10 +277,14 @@ def normalize(img):
raise RuntimeError(imageops_err)
return imageops.normalize(image_from_data(img))
def quantize(img, colors=256, dither=True, palette=''):
''' Quantize the image to contain a maximum of `colors` colors. By default a palette is chosen automatically,
if you want to use a fixed palette, then pass in a list of color names in the `palette` variable. If you,
specify a palette `colors` is ignored. For example: palette='red green blue #eee' '''
def quantize(img, max_colors=256, dither=True, palette=''):
''' Quantize the image to contain a maximum of `max_colors` colors. By
default a palette is chosen automatically, if you want to use a fixed
palette, then pass in a list of color names in the `palette` variable. If
you, specify a palette `max_colors` is ignored. Note that it is possible
for the actual number of colors used to be less than max_colors.
For example: palette='red green blue #eee' '''
if imageops is None:
raise RuntimeError(imageops_err)
img = image_from_data(img)
@ -288,7 +292,7 @@ def quantize(img, colors=256, dither=True, palette=''):
img = blend_image(img)
if palette and isinstance(palette, basestring):
palette = palette.split()
return imageops.quantize(img, colors, dither, [QColor(x).rgb() for x in palette])
return imageops.quantize(img, max_colors, dither, [QColor(x).rgb() for x in palette])
# Optimization of images {{{