Python PIL eval() method

Python PIL eval() Method

PIL is the Python Imaging Library, which provides image editing capabilities for the Python interpreter. The image module provides a class of the same name that represents a PIL image. The module also provides factory functions, including functions for loading images from files and creating new images.

PIL.Image.eval() applies the function (which should have one argument) to each pixel in the given image. If the image has more than one band, the same function is applied to each band. Note that the function is evaluated once for each possible pixel value, so you cannot use random components or other generators.

Syntax: PIL.Image.eval(image, *args)

Arguments:
image – The input image.
function – A function object that accepts a single integer argument.

Return type. An image.

Images used:
Python PIL eval() method


  
# Importing Image module from PIL package
from PIL import Image
  
#creating an image object
im2 = Image.open(r"C:UsersSystem-PcDesktoplion.PNG")
  
# applying the eval method
im3 = Image.eval(im2, (lambda x: 254 - x * 15))
  
im3.show()

Output:

Python PIL eval() Method

Another Example: Here we change the parameter values for another image.

Images used-
Python PIL eval() method

# Importing Image module from PIL package
from PIL import Image
  
#creating an image object
im2 = Image.open(r"C:UsersSystem-PcDesktopeval2image.PNG")
  
# applying the eval method
im3 = Image.eval(im2, (lambda x: 240 - x * 12))
  
im3.show()

Output:
Python PIL eval() method

Leave a Reply

Your email address will not be published. Required fields are marked *