Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python how to change the image pixel values

from PIL import Image

def change_image_pixel_values(imaged_path,by_value):
    # Import an image from directory:
    input_image = Image.open(imaged_path)
    
    # Extracting pixel map:
    pixel_map = input_image.load()
    
    # Extracting the width and height 
    # of the image:
    width, height = input_image.size
    
    # taking half of the width:
    for i in range(width):
        for j in range(height):
            
            # getting the RGB pixel value.
            red, green, blue, p = input_image.getpixel((i, j))

            red -= by_value
            green -= by_value
            blue -= by_value
            # setting the pixel value.
            pixel_map[i, j] = (red,green,blue)
    
    # Saving the final output
    # as "grayscale.png":
    input_image.save(imaged_path, format="png")
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #change #image #pixel #values
ADD COMMENT
Topic
Name
7+4 =