oclcq.py Opencl-accelerated colour quantization in python

version:1.0.0-beta
author:Alejandro López Correa
contact:ten.akips@cla
license:MIT License

A python module to compute fast colour quantization using opencl to accelerate k-means algorithm. It works with PIL images with modes RGB, RGBA and L. It does not support float or int pixel formats yet. This module is able to both generate RGB/RGBA/greyscale images with a reduced colour count, and to palettize images.
Tested with python 2.7 and 3.3.
Requires pyopencl, numpy and either PIL or pillow.

Quick example:
import oclcq
import PIL.Image

im = PIL.Image.open( 'test.png' )

imo = oclcq.quantize_image( im, colourCount=1024 )
imo.save( 'out.png' )

imo = oclcq.palettize_image( im, colourCount=30 )
imo.save( 'out_pal.png' )

documentation
quantize_image( im, **kwArgs )

Returns a new image with at most given colour count, and with the same image mode.

im: PIL image, supported modes are RGB, RGBA and L

optional args:
* vcentroid: palette to use. 2d array, each index in the first axis is a colour. This argument excludes all others.
* colourCount: int >= 2, defaults to 256
* threshold: float > 0, threshold to stop kmeans clustering
* iterationCount: int >= 1, defaults to 1. Number of runs of the kmeans clustering, a larger value helps finding a better quantization

palettize_image( im, **kwArgs )

Returns a new image with at most given colour count, and with 'P' image mode, that is, a palettized image.

im: PIL image

optional args:
* vcentroid: palette to use. 2d array, each index in the first axis is a colour. This argument excludes all others.
* colourCount: int in range [2,256], defaults to 256
* threshold: float > 0, threshold to stop kmeans clustering
* iterationCount: int >= 1, defaults to 1. Number of runs of the kmeans clustering, a larger value helps finding a better quantization

download / installation
For now, the only option available is direct download of source code (single file).

oclcq.py
to-do list