Quantizing with PIL

Let's use the PIL Image module's convert() function for color quantization, with the mode and the color argument as the maximum number of possible colors. We'll also use the SciPy stats module's signaltonoise() function to find the Signal-to-Noise Ratio (SNR) of an image (parrot.jpg), which is defined as the mean divided by the standard deviation of the image array:

im = Image.open('../images/parrot.jpg')pylab.figure(figsize=(20,30))num_colors_list = [1 << n for n in range(8,0,-1)]snr_list = []i = 1for num_colors in num_colors_list:    im1 = im.convert('P', palette=Image.ADAPTIVE, colors=num_colors)    pylab.subplot(4,2,i), pylab.imshow(im1), pylab.axis('off')    snr_list.append(signaltonoise(im1, axis=None)) pylab.title('Image ...

Get Hands-On Image Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.