Introduction :
Hey Coders , In this tutorial we are going to learn about how to make an image blue using python. We will be doing this by using Pillow. So let us know about Pillow.
Pillow :
Pillow is a Python Imaging Library that adds image processing capabilities to your Python interpreter. It allows you to open, manipulate, and save many different image file formats.
Step 1 : Installing Pillow
Before doing all the steps we need to install the pillow library to the system . To install that we need to use Command prompt for windows , Terminal on MacOS and Linux.
To install we just need to write a simple command that is ‘ pip install pillow ‘ in our command line
pip install pillow
And here ‘ pip ‘ is a package manager for Python that allows you to install and manage additional libraries and dependencies.
By installing Pillow, you equip your Python environment with the tools needed for image manipulation, which is essential for creating a blue-tinted image in subsequent steps.
Step 2 :Open the Image
For opening the image at first we need to import the ‘ image class from ‘ the ‘ PIL ‘ (Python Imaging Library) module, which is provided by Pillow. To do that we just need to write a very simple line i our program that is ‘ from PIL import Image ‘ which will be like
from PIL import Image
Then we need to open he image for that we have to Use the ‘ Image.open() ‘ method to open an image file. And assign the path of the image so that the desired image will be open that we want to make blue .
image = Image.open('path_to_your_image.jpg')
Here in the place of ‘ path_to_your_image.jpg ‘ yu need to type or enter the actual path of the image
After importing or opening the image just verify that if your image has been loaded successfully or not for that we just need to simply print the image ,
print(image) image.show()
Here , ‘ print(image) ‘ Prints a brief summary of the image, including its format, size, and mode (e.g., RGB). And ‘ image.show() ‘ Opens the image in the default image viewer on your computer, allowing you to visually confirm that the image was loaded correctly.
By successfully loading the image into your script, you can proceed with further image processing tasks such as applying a blue tint.
Step 3 : Split the Image into Color Channels
In this step, we will split the image into its individual color channels: Red (R), Green (G), and Blue (B). This allows you to manipulate each channel separately. To do that we just need to write a simple line in our program that is ‘ r, g, b = image.split() ‘ .
r, g, b = image.split()
This steps works in 2 parts those are :-
- Splitting the image
- Assigning the Channels
‘ image.split() ‘ is the method that method breaks down the original image into three separate grayscale images, each representing the intensity values of the Red, Green, and Blue channels. And ‘ r, g, b ‘ are the variables hold the grayscale images of the Red, Green, and Blue channels, respectively. You can now manipulate these channels independently.
By splitting the image into its individual color channels, you can manipulate each channel separately. In the next step, you’ll use this capability to create a blue-tinted image by combining these channels in a specific way.
Step 4 : Create a Blue-tinted Image
In this step, we will create a new image that is tinted blue by combining the individual Red (R), Green (G), and Blue (B) channels. You’ll set the Red and Green channels to zero, and keep the Blue channel unchanged.
At first , We need to Modify all the channels according to which the blue image will form. To do that we need to write a line in our code that is ‘ zero_channel = r.point(lambda i: 0) ‘ .
zero_channel = r.point(lambda i: 0)
Here , ‘ zero_channel ‘ variable holds the modified Red and Green channels, where all pixel values are set to zero. And ‘ point() ‘ method applies a function to each pixel in the image.
The lambda function ‘ lambda i: 0 ‘ sets every pixel value to 0, effectively creating a black image. This is done for the Red and Green channels to remove their influence on the final image.
After modifying the images we need to merge the channels for that we just need to write ‘ blue_tinted_image = Image.merge(“RGB”, (zero_channel, zero_channel, b)) ‘ in our program.
where the ‘ merge() ‘ method combines the three specified channels into a new image. The mode “RGB” indicates that the image should be an RGB image. The first two channels are the zeroed-out Red and Green channels, and the third channel is the original Blue channel.
blue_tinted_image = Image.merge("RGB", (zero_channel, zero_channel, b))
By setting the Red and Green channels to zero and keeping the Blue channel unchanged, you create an image that only shows the blue components of the original image, resulting in a blue-tinted effect.
Step 5 : Save the New Image
In this step, you will save the blue-tinted image that you created in the previous step to a file on your computer. To save the new tinted image we just need to write a line that is ‘ blue_tinted_image.save(‘path_to_save_new_image.jpg’) ‘ .
blue_tinted_image.save('path_to_save_new_image.jpg')
Here , the ‘ save() ‘ method method writes the image to a file. The argument is the file path where the image will be saved. Pillow will determine the file format from the file extension you provide. And ‘ path_to_save_new_image.jpg ‘ is just an exampler statement and in place of this statement we need to write the path where the new image is to be saved. This is simply the location on your computer where you want to save the new image file. Make sure to include the appropriate file extension (e.g., .jpg, .png, etc.).