PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.
Image.quantize() Convert the image to ‘P’ mode with the specified number of colors.
Syntax: Image.quantize(colors=256, method=None, kmeans=0, palette=None)
Parameters:
colors – The desired number of colors, <= 256
method – 0 = median cut 1 = maximum coverage 2 = fast octree
kmeans – Integer
palette – Quantize to the PIL.ImagingPalette palette.Returns type: An Image object.
Image Used:

# Importing Image module from PIL package from PIL import Image import PIL # creating a image object (main image) im1 = Image.open(r"C:\Users\System-Pc\Desktop\flower1.jpg") # quantize a image im1 = im1.quantize(256) # to show specified image im1.show() |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python PIL | GaussianBlur() method
- Python PIL | Kernel() method
- Python PIL | BoxBlur() method
- Python PIL | getbands() method
- Python PIL | logical_and() and logical_or() method
- Python PIL | ImageChops.subtract() method
- Python PIL | UnsahrpMask() method
- Python PIL | ImageChops.subtract() and ImageChops.subtract_modulo() method
- Python PIL | Image.crop() method
- Python PIL | ImageOps.expand() method
- Python PIL | ImageEnhance.Color() and ImageEnhance.Contrast() method
- Python PIL | ImageGrab.grabclipboard() method
- Python PIL | RankFilter() method
- Python PIL | ImageChops.screen() and ImageChops.offset() method
- Python PIL | MedianFilter() and ModeFilter() method
- Python PIL | Image.new() method
- Python PIL | getbands() and getextrema() method
- Python PIL | paste() and rotate() method
- Python PIL | ImageChops.darker() method
- Python PIL | Image.split() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

