Skip to main content

Image Editing Software | Applying Color Filters on Image

 Hello friends! Welcome to the last post on Image Editing Software. In this post, you will be learning how to apply color filters to an image. We will be doing this using the OpenCV library package. You can also check out the video below. 


So let's get started. We will start by importing Opencv and Numpy to our code. 

import cv2
import numpy as np

And now we will start writing the callback methods for applying filters. Let us start with the yellowButton_callback() method. 

def yellowButton_callback():
opencvImage = cv2.cvtColor(np.array(originalImage), cv2.COLOR_RGB2BGR)
opencvImage[:, :, 0] = 20
global outputImage
outputImage = Image.fromarray(cv2.cvtColor(opencvImage, cv2.COLOR_BGR2RGB))
dispayImage(outputImage)

Alright. Let us understand the above code line by line. To apply a color filter I have to manipulate the image array. So I need the image in an array form. np.array(originalImage) converts the image that we read using the PIL library into a 3D array. Now, since OpenCV reads images in BGR form, I had to convert them from RGB(PIL uses RGB format) to BGR using cv2.COLOR_RGB2BGR. Thus opencvImage holds the image data in array form and in BGR format.

Now, let's go to the second line. Here I have just assigned a constant value to channel 0(Blue channel) of the image. But why 20? Well, I got that value by experimenting with the image array. You are free to assign whichever value you want. You will see different kinds of color filters being applied to the image. 

Then we define the outputImage variable to store the image that will be displayed on the GUI window and for saving purposes. Now, remember we are using PIL to display and save images. So we have to convert back this opencvImage into PIL compatible format. For that, we have to convert the opencvImage from BGR to RGB format using cv2.COLOR_BGR2RGB method. And again convert the image array into PIL compatible format using Image.fromarray() method. At last displayImage() method is called to display the image on the GUI window. 

Similarly, you can write other color filter callback methods. Only the array manipulation line will change. Below are all the callback methods for your reference. 

def blueButton_callback():
opencvImage = cv2.cvtColor(np.array(originalImage), cv2.COLOR_RGB2BGR)
opencvImage[:, :, 2] = 100
global outputImage
outputImage = Image.fromarray(cv2.cvtColor(opencvImage, cv2.COLOR_BGR2RGB))
dispayImage(outputImage)

def pinkButton_callback():
opencvImage = cv2.cvtColor(np.array(originalImage), cv2.COLOR_RGB2BGR)
opencvImage[:, :, 1] = 100
global outputImage
outputImage = Image.fromarray(cv2.cvtColor(opencvImage, cv2.COLOR_BGR2RGB))
dispayImage(outputImage)

def orangeButton_callback():
opencvImage = cv2.cvtColor(np.array(originalImage), cv2.COLOR_RGB2BGR)
opencvImage[:, :, 2] = 200
global outputImage
outputImage = Image.fromarray(cv2.cvtColor(opencvImage, cv2.COLOR_BGR2RGB))
dispayImage(outputImage)

def noneButton_callback():
pass


The Image Editing Software is now complete.

Now, You can run this code and use this basic level Image Editing Software. You can also try to add more features to this software. 

Enjoy Coding.....!!!

For full code click here.







Comments

Popular posts from this blog

Iris Detection | Python | OpenCv

 Hello there! Welcome to another blog. In this blog you are going to learn to detect iris using OpenCv python. Here is the video in case you missed it. So, let's get started. We will start by importing the necessary libraries. import cv2 import numpy as np Now, let us import the face and eye classifier files and set the camera resolution as follows. eye = cv2.CascadeClassifier( 'haarcascade_eye.xml' ) face = cv2.CascadeClassifier( 'haarcascade_frontalface_alt.xml' ) Kernal = np.ones(( 3 , 3 ) , np.uint8) #Declare kernal for morphology cap = cv2.VideoCapture( 0 ) cap.set(cv2.CAP_PROP_FRAME_WIDTH , 320 ) ##Set camera resolution cap.set(cv2.CAP_PROP_FRAME_HEIGHT , 240 ) In a while loop let us capture an image frame, flip it(in case your camera captures inverted images) and convert it into a gray scale image. ret , frame = cap.read() ##Read image frame frame = cv2.flip(frame , + 1 ) ##Flip the image in case your camera

Object Distance Calculation Using Contour Area Method In Python - Opencv

Today we will discuss how you can find the distance of an object from the camera using python OpenCV. Check out the video below. Before we continue, you should know how to detect a colored object. Click this link to check out my previous blog on object detection and tracking. I hope after checking out my previous blog, you are able to write your own code to detect and track objects. We will take forward the Object detection and tracking code to find the distance of an object from the camera. So let's start. Let us first understand the principle using which we will find the distance of the object from the camera. Principle:- Area enclosed by the contours of an object decreases as the object moves farther from the camera. This simply means that, if your object is near to the camera, the object will appear bigger. Thus the pixel area occupied by the object will be very large. As you move the object farther from the camera, the object size in the image will start to d

Object Detection And Tracking using Python - Opencv

Let us discuss today how you can detect and track an object in real-time. We will be using Python language and Opencv library for this purpose. Check out the video below. If you have read my previous blogs, you can directly skip down to the contour part. As usual, we need to make a few assumptions for the proper working of this application. This background is always static i.e. there is no addition or subtraction of objects in the background scene. The background-color is always constant. It does not change with time. The object that will be used for writing/painting is of a different color than the background to give us sufficient contrast between foreground and background. We are ready to begin now. Let us start by installing necessary python libraries for our project using  pip install.  We will be needing  Numpy  and  Opencv  libraries. Now create a python project and create a new script. Import the required libraries into python script as shown below. import cv2