How to manipulate the pixel values of an image using Python ? Last Updated : 28 Apr, 2021 Comments Improve Suggest changes Like Article Like Report It is well known that a color image is a collection of various pixels. And if we change the pixel value the image will turn into an image of a different color. Doing this tedious task manually is awful as an image might contain millions of pixels. So we will write a Python script that will easily complete this task. While developing predictive models of image data we sometimes need to manipulate the image. And for this purpose python has an amazing library named Python Imaging Library(PIL). This library contains some method with which we can extract the pixelmap of an image and simply with the help of loops we can iterate over each of its pixels and change its pixel value according to our need. A pixel is the smallest base component of an image and similarly, a pixel map can be thought of as a matrix of pixels that represents an image. ApproachFirst, we need an image file as input. This image file can either be created through the Image.new() method or be imported from the local machine through the Image.open() method. Both the cases have been shown in the below example. (Not mandatory but for our convenience, we have saved the image with the name of "input.png" especially to look at the difference.)Secondly, we need to extract the pixel map of the input image(the matrix of pixel values) with the help of the Image.load() method so that we can manipulate our desired pixel. The Image.size method returns the width and height(column and row) of the image(pixelmap or matrix). Then with the help of loops, we will iterate and change our desired value of the pixel.Finally, after updating or changing the pixel value we will get the output image. (Again not mandatory but for our convenience, we will save the output image with the name of "output.png" with the help of the Image.save() method. We can also see the image on the output screen using the Image.show() method. Example 1: Use an image from the local machine and turn half of it into a grayscale image The average formula to change an image into a grayscale image: G = (R+G+B) / 3 The above formula is theoretically correct but a more improved formula(The weighted method, also called luminosity method, weighs red, green, and blue according to their wavelengths) is as follows: G = (0.299R + 0.587G + 0.114B) Input image: Input image Python3 from PIL import Image # Import an image from directory: input_image = Image.open("gfg.png") # 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//2): for j in range(height): # getting the RGB pixel value. r, g, b, p = input_image.getpixel((i, j)) # Apply formula of grayscale: grayscale = (0.299*r + 0.587*g + 0.114*b) # setting the pixel value. pixel_map[i, j] = (int(grayscale), int(grayscale), int(grayscale)) # Saving the final output # as "grayscale.png": input_image.save("grayscale", format="png") # use input_image.show() to see the image on the # output screen. Output: Output image. Note: Here the half of the image has been converted to grayscale, but the full can be done using the same code just by changing (width//2) to (width). To know more about this refer Image.getpixel() method. Example 2: Manipulate pixel values Input Image: Input image. Python3 from PIL import Image # Create an image as input: input_image = Image.new(mode="RGB", size=(400, 400), color="blue") # save the image as "input.png" #(not mandatory) input_image.save("input", format="png") # Extracting pixel map: pixel_map = input_image.load() # Extracting the width and height # of the image: width, height = input_image.size z = 100 for i in range(width): for j in range(height): # the following if part will create # a square with color orange if((i >= z and i <= width-z) and (j >= z and j <= height-z)): # RGB value of orange. pixel_map[i, j] = (255, 165, 0) # the following else part will fill the # rest part with color light salmon. else: # RGB value of light salmon. pixel_map[i, j] = (255, 160, 122) # The following loop will create a cross # of color blue. for i in range(width): # RGB value of Blue. pixel_map[i, i] = (0, 0, 255) pixel_map[i, width-i-1] = (0, 0, 255) # Saving the final output # as "output.png": input_image.save("output", format="png") # use input_image.show() to see the image on the # output screen. Output: Output image Comment More infoAdvertise with us Next Article How to manipulate the pixel values of an image using Python ? tenacious39 Follow Improve Article Tags : Python Python-pil Practice Tags : python Similar Reads Python Pillow Tutorial sinceDigital Image processing means processing the image digitally with the help of a computer. Using image processing we can perform operations like enhancing the image, blurring the image, extracting text from images, and many more operations. There are various ways to process images digitally. He 15+ min read Introduction to PillowPython: Pillow (a fork of PIL)Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aids in editing, creating and saving images. Support for Python Imaging Library got discontinued in 2011, but a project named pillow forked 4 min read Installation and setupHow to Install Pillow on MacOS?In this article, we will learn how to install Pillow in Python on MacOS. Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. Installation:Method 1: Using pip to install Pillow Follow the below steps to install the Pillow package on macOS using pip: 2 min read How to Install PIL on Windows?In this article, we will look into the various methods of installing the PIL package on a Windows machine. Prerequisite:Python PIP or Ananconda (Depending upon your preference)For PIP Users: Open up the command prompt and use the below command to install the PIL package: pip install Pillow The follo 1 min read How to Install PIL on Linux?PIL is an acronym for Python Image Library. It is also called Pillow. It is one of the most famous libraries for manipulating images using the python programming language. It is a free and open-source Python library. Installing PIL on Linux:Method 1: Using PIP command: Step 1: Open up the Linux term 1 min read Loading and Saving ImagesPython PIL | Image.save() methodPIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, 3 min read Python PIL | Image.show() methodPIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, 1 min read Finding Difference between Images using PILPython interpreter in itself doesn't contain the ability to process images and making out a conclusion to it. So, PIL(Python Imaging Library) adds image processing powers to the interpreter. PIL is an open-source library that provides python with external file support and efficiency to process image 2 min read Image Manipulation BasicsPython Pillow - Working with ImagesIn this article, we will see how to work with images using Pillow in Python. We will discuss basic operations like creating, saving, rotating images. So let's get started discussing in detail but first, let's see how to install pillow. Installation To install this package type the below command in t 4 min read Python PIL | Image.resize() methodPIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, 4 min read Python Pillow - Flip and Rotate ImagesPrerequisites: Pillow Python Pillow or PIL is the Python library that provides image editing and manipulating features. The Image Module in it provides a number of functions to flip and rotate images. image.transpose() is the function used to rotate and flip images with necessary keywords as paramet 2 min read Python PIL | paste() and rotate() methodPIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.Image.paste() method is used to paste an image on another image. This is where the new() method comes in handy. Syntax: PIL.Image.Image.paste(image_1, image_2, box=None, mask=None) OR i 2 min read Adjusting Image PropertiesChange image resolution using Pillow in PythonPrerequisites: Python pillow PIL is the Python Imaging Library which provides the python interpreter with an in-depth file format support, an efficient internal representation, and fairly powerful image processing capabilities. Changing the resolution of an image simply means reducing or increasing 2 min read Image Enhancement in PILThe Python Imaging Library(PIL) adds powerful image processing capabilities. It provides immense file format support, an efficient representation, and fairly powerful image processing capabilities. The core image library is intended for fast access to data stored in very few basic pixel formats. It 4 min read Image Filtering and EffectsPython Pillow - Blur an ImageBlurring an image is a process of reducing the level of noise in the image, and it is one of the important aspects of image processing. In this article, we will learn to blur an image using a pillow library. To blur an image we make use of some methods of ImageFilter class of this library on image o 2 min read How to merge images with same size using the Python 3 module pillow?In this article, the task is to merge image with size using the module pillow in python 3. Python 3 module pillow : This is the update of Python Imaging Library. It is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and savi 2 min read Drawing on ImagesAdding Text on Image using Python - PILIn Python to open an image, image editing, saving that image in different formats one additional library called Python Imaging Library (PIL). Using this PIL we can do so many operations on images like create a new Image, edit an existing image, rotate an image, etc. For adding text we have to follow 2 min read Python Pillow - ImageDraw ModulePython's Pillow which is a fork of the discontinued Python Imaging Library (PIL) is a powerful library that is capable of adding image processing capabilities to your python code. Pillow offers many modules that ease the process of working and modifying images. In this article, we will have a look a 5 min read Python Pillow - Colors on an ImageIn this article, we will learn Colors on an Image using the Pillow module in Python. Let's discuss some concepts: A crucial class within the Python Imaging Library is the Image class. It's defined within the Image module and provides a PIL image on which manipulation operations are often administere 4 min read Image TransformationsHow to rotate an image using Python?Image rotation in Python rotates an image around its centre by a specified angle using forward or inverse methods. When the angle isnât a multiple of 90 degrees, parts of the image may move outside the visible boundaries and get clipped. To avoid losing important content during rotation you need pro 3 min read Python PIL | Image.transform() methodPIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, 1 min read Working with Image MetadataHow to extract image metadata in Python?Prerequisites: PIL Metadata stands for data about data. In case of images, metadata means details about the image and its production. Some metadata is generated automatically by the capturing device. Some details contained by image metadata is as follows: HeightWidthDate and TimeModel etc. Python h 2 min read Python | Working with the Image Data Type in pillowIn this article, we will look into some attributes of an Image object that will give information about the image and the file it was loaded from. For this, we will need to import image module from pillow. Image we will be working on : size() method - It helps to get the dimensions of an image. IMG = 2 min read Like