Skip to main content

Image Editing Software | Changing Brightness And Contrast

 Hello! In this post, you will be learning how to change the brightness and contrast of an image using the PIL library package. You can check out the video below to learn more. 





We will continue to build out previous code. Let us start by importing the required library package.

from PIL import ImageEnhance

The ImageEnhance package will be used to change the brightness and contrast of an image. Now, we will be writing a few lines to the brightness_callback() method. 

def brightness_callback(brightness_pos):
brightness_pos = float(brightness_pos)
global outputImage
enhancer = ImageEnhance.Brightness(originalImage)
outputImage = enhancer.enhance(brightness_pos)
dispayImage(outputImage)

brightness_callback() method requires an argument to be passed. This argument holds the current position of the slider and is of string type. The ImageEnhance library package needs a variable of float type. So that is why in the first line we have converted the brightness_pos variable to float type. In the next line, we have declared a global outputImage variable. This is because while saving the image we want the output image to be saved and not the original image(unlike the previous post where we saved the original image). The same variable will be used throughout all the callback methods.

ImageEnhance.Brightness() method is used to control the brightness of an image. enhancer.enhance() changes the brightness by a given factor. This is where the brightness_pos variable comes into the picture. A value of 0.0 will give you a black image and a value of 1.0 will give you the original image. The edited image is stored in outputImage variable. And at last, we call the displayImage() method for displaying the edited image on the GUI window.

Similar to the previous case we will be writing the code for contrast_callback() method.

def contrast_callback(contrast_pos):
contrast_pos = float(contrast_pos)
global outputImage
enhancer = ImageEnhance.Contrast(originalImage)
outputImage = enhancer.enhance(contrast_pos)
dispayImage(outputImage)

The only difference here is, for changing the contrast we are using ImageEnhance.Contrast() method. The rest of the code remains the same.

For saving the edited image, you have to make a little change in the code. Instead of originalImage you have to replace it with outputImage as shown below.

def saveButton_callback():
savefile = filedialog.asksaveasfile(defaultextension=".jpg")
outputImage.save(savefile)

That is it. Your code is ready to run. 

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