- 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 - ImageChops.constant() Function
The PIL.ImageChops.constant function is used to fill a channel of an image with a given grey level. This grey level value should be within the valid range for pixel values (e.g., 0 to 255 for an 8-bit image).
Syntax
Following is the syntax of the function −
PIL.ImageChops.constant(image, value)
Parameters
Here's a brief explanation of the parameters −
image − The input image.
value − Integer value that represents the grey level value to fill the channel.
Return Value
The return type of this function is an Image.
Examples
Example 1
Here's an example that demonstrates the pixel values before and after using ImageChops.constant() function.
from PIL import Image, ImageChops import numpy as np # Create a simple RGB image with some variation in pixel values original_image = Image.fromarray(np.array([(35, 64, 3), (255, 0, 0), (255, 255, 0), (255, 255, 255), (164, 0, 3)]), mode="RGB") print("Original Pixel values at (0, 0):", original_image.getpixel((0, 0))) # Fill the red channel with a constant grey level value of 128 result = ImageChops.constant(original_image, value=100) print("Pixel values of the result at (0, 0) after constant:", result.getpixel((0, 0)))
Output
Original Pixel values at (0, 0): (35, 0, 0) Pixel values of the result at (0, 0) after constant: 100
Example 2
In this example, the ImageChops.constant() function is used to fill the original image of PNG type with a constant grey level value of 128.
from PIL import Image, ImageChops # Open an image file original_image = Image.open('Images/pillow-logo-w.png') # Fill the red channel with a constant grey level value of 128 result = ImageChops.constant(original_image, value=128) # Display the input and resulting images original_image.show() result.show()
Output
Input Image
Output Image
Example 3
The following example demonstrates how to modify the image pixel values by filling it with a constant grey level value of 100.
from PIL import Image, ImageChops # Open an image file original_image = Image.open('Images/butterfly.jpg') print("Original Pixel values at (0, 0):", original_image.getpixel((0, 0))) # Fill the red channel with a constant grey level value of 128 result = ImageChops.constant(original_image, value=100) print("Pixel values of the result at (0, 0) after constant:", result.getpixel((0, 0))) # Display the input and resulting images original_image.show() result.show()
Output
Input Image 1
Output Image