.eval()
Published Mar 28, 2025
Contribute to Docs
The .eval()
method in Pillow processes an image by applying a given function to each pixel. This is useful for pixel-level transformations like adjusting brightness, inverting colors, or applying custom filters.
Syntax
Image.eval(image, function)
image
: The input image object to process.function
: A function/lambda that takes one integer argument (the pixel value) and returns a modified integer.
Example
The example below converts the input image into a new image, where each pixel is transformed by a function:
from PIL import Image# Open an imageimg = Image.open('media/pillow-original.jpeg').convert("RGB") # Ensure RGB mode# Define a function to invert pixel valuesdef invert_pixel(x):return 255 - x # Works for grayscale and individual RGB channels# Apply the function to each pixelnew_img = Image.eval(img, invert_pixel)# Display the new imagenew_img.show()
In this example:
x
: Represents the pixel intensity value, which typically ranges from 0-255 (for 8-bit grayscale or RGB images).255 - x
: Subtracts the pixel value from 255, effectively inverting its brightness: 0 → 255 (pure black becomes pure white), 255 → 0 (pure white becomes pure black), 100 → 155 (mid-gray becomes a lighter gray).
Here is the original image:
Here is the updated image:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pillow on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours