Friday, September 5, 2025
HomeLanguagesPython PIL | Image.quantize() method

Python PIL | Image.quantize() method

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:

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS