- Python Pillow Tutorial
- Python Pillow - Home
- Python Pillow - Overview
- Python Pillow - Environment Setup
- Basic Image Operations
- Python Pillow - Working with Images
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Adding Borders to Images
- Python Pillow - Identifying Image Files
- Python Pillow - Merging Images
- Python Pillow - Cutting and Pasting Images
- Python Pillow - Rolling an Image
- Python Pillow - Writing text on image
- Python Pillow - ImageDraw Module
- Python Pillow - Concatenating two Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Creating a Watermark
- Python Pillow - Image Sequences
- Python Pillow Color Conversions
- Python Pillow - Colors on an Image
- Python Pillow - Creating Images With Colors
- Python Pillow - Converting Color String to RGB Color Values
- Python Pillow - Converting Color String to Grayscale Values
- Python Pillow - Change the Color by Changing the Pixel Values
- Image Manipulation
- Python Pillow - Reducing Noise
- Python Pillow - Changing Image Modes
- Python Pillow - Compositing Images
- Python Pillow - Working with Alpha Channels
- Python Pillow - Applying Perspective Transforms
- Image Filtering
- Python Pillow - Adding Filters to an Image
- Python Pillow - Convolution Filters
- Python Pillow - Blur an Image
- Python Pillow - Edge Detection
- Python Pillow - Embossing Images
- Python Pillow - Enhancing Edges
- Python Pillow - Unsharp Mask Filter
- Image Enhancement and Correction
- Python Pillow - Enhancing Contrast
- Python Pillow - Enhancing Sharpness
- Python Pillow - Enhancing Color
- Python Pillow - Correcting Color Balance
- Python Pillow - Removing Noise
- Image Analysis
- Python Pillow - Extracting Image Metadata
- Python Pillow - Identifying Colors
- Advanced Topics
- Python Pillow - Creating Animated GIFs
- Python Pillow - Batch Processing Images
- Python Pillow - Converting Image File Formats
- Python Pillow - Adding Padding to an Image
- Python Pillow - Color Inversion
- Python Pillow - M L with Numpy
- Python Pillow with Tkinter BitmapImage and PhotoImage objects
- Image Module
- Python Pillow - Image Blending
- Python Pillow Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - Function Reference
- Python Pillow - Useful Resources
- Python Pillow - Discussion
Python Pillow - ImageOps.posterize() Function
The PIL.ImageOps.posterize function is used to reduce the number of bits for each color channel in an image, effectively limiting the color palette.
Syntax
Following is the syntax of the function −
PIL.ImageOps.posterize(image, bits)
Parameters
Here are the details of this function parameters −
image − The image to posterize.
bits − The number of bits to keep for each channel (1-8). The value of bits determines the number of color levels for each channel, and it ranges from 1 to 8.
Return Value
The function returns a new image object where the input image has been posterized with the specified number of bits.
Examples
Example 1
Here’s an example that demonstrates how to create a new image with just 4-bits per channel.
from PIL import Image, ImageOps # Open an image file input_image = Image.open("Images/flowers.jpg") # Posterize the image with 4 bits per channel posterized_image = ImageOps.posterize(input_image, bits=4) # Display the original and posterized images input_image.show() posterized_image.show()
Output
Input Image
Output Image
Example 2
Here's another example using the PIL.ImageOps.posterize function with a different image.
from PIL import Image, ImageOps # Open an image file input_image = Image.open("Images/Tajmahal_2.jpg") # Posterize the image with 2 bits per channel posterized_image = ImageOps.posterize(input_image, bits=2) # Display the original and posterized images input_image.show() posterized_image.show()
Output
Input Image
Output Image
Example 3
In this example, the code performs posterization with different bit values to the image. And we use the Matplotlib to display the results.
from PIL import Image, ImageOps import matplotlib.pyplot as plt # Open the original image original_image = Image.open('Images/flowers_1.jpg') # Create subplots for original and posterized images num_bits_list = [1, 2, 3, 4, 5, 6, 7, 8] fig, axes = plt.subplots(3, 3, figsize=(10, 15)) # Display the original image axes[0, 0].imshow(original_image) axes[0, 0].set_title('Original Image') axes[0, 0].axis('off') # Perform posterization with different bit values and display the images for idx, num_bits in enumerate(num_bits_list, start=1): posterized_image = ImageOps.posterize(original_image, bits=num_bits) # Display the posterized images axes[idx // 3, idx % 3].imshow(posterized_image) axes[idx // 3, idx % 3].set_title(f'Posterized {num_bits} bits') axes[idx // 3, idx % 3].axis('off') plt.tight_layout() plt.show()