.putdata()
The .putdata()
method in Pillow is used to modify the pixel data of an image, replacing the current pixel values with new values from a sequence or iterable (such as a list or tuple). It’s often used in combination with the .getdata()
method, which retrieves the pixel data from the image, allowing you to modify the data before putting it back with .putdata()
.
The .putdata()
method modifies the image in-place and does not return a new image object. It will return None
, meaning the output will have NoneType
if assigned to a variable.
It’s an essential method in the Image
module of the Pillow library.
Syntax
Image.putdata(pixel_data, scale=1.0, offset=0.0)
Parameters:
data
: A flattened sequence object containing pixel values to be assigned to the image. The values start from the upper-left corner (0, 0) of the image, proceed across rows, and continue until the sequence ends or the image size is reached.scale
: A scaling factor for the pixel data. Each pixel value is multiplied by this scale before being assigned.offset
: A value added to each pixel after scaling.
Return value:
- The
.putdata()
method does not return anything. It modifies the image’s pixel data in place and returnsNone
.
Example 1: Dim an Image Using .putdata()
with Scale and Offset in Pillow
Here is the image used as an example to perform the .putdata()
method:
The following example demonstrates how to use the scale
and offset
parameters in the .putdata()
method to adjust the pixel values and produce a dimmed image:
from PIL import Image# Load the imagepy_img = Image.open('pillow_python_logo.jpg')# Get the sequenced pixel data from the imagepix_data = list(py_img.getdata())# Use .putdata method to modify and put back the pixel data directlypy_img.putdata(pix_data, scale=0.8, offset=0) # Dim the image by scaling pixel values down# Show the image outputpy_img.show()
The output of this code will be a dimmed version of the image:
Example 2: Enhance Image Contrast Using .putdata()
in Pillow
This example demonstrates how to use the .putdata()
method to adjust the contrast of the original image by modifying the pixel values with a scale
and offset
:
from PIL import Image# Load the imagepy_img = Image.open('pillow_python_logo.jpg')# Get the sequenced pixel data from the imagepix_data = list(py_img.getdata())# Use .putdata method to modify and put back the pixel data directlypy_img.putdata(pix_data, scale=1.5, offset=-50) # Increase contrast by scaling and offsetting# Show the image outputpy_img.show()
The output image will show increased contrast:
The .putdata()
method works by accessing the pixel from the top-left corner of the image, modifying the pixel value using the formula pixel = value * scale + offset
, and then moving to the next pixel to the right, continuing from left to right, top to bottom.
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