Skip to main content

Image Editing Software | GUI Designing | Creating Buttons



Hello Friends! We are going to develop an Image Editing Software in python. We will be using various libraries like Tkinter, PIL, and OpenCV. We will be developing several features like brightness adjustment, contrast adjustment, applying various filters on the image, etc. So why wait? Let's get started. Make sure to check out the video below before starting.



We will start by designing the GUI and later on, we will add the required functionalities to it for image editing. In this tutorial, we will be creating a window that will hold 3 buttons(Import, Save, Close).

To create the GUI we will start by importing the Tkinter in our python file as shown below.

import tkinter as tk

To create a window we will have to call a method called tk.Tk().

window = tk.Tk()

So the window is ready. But if you run the above code, you won't see anything appearing on the screen. This is because the program has finished execution. You will have to add a loop to hold the window on the screen. For that, we will call the method tk.mainloop().

tk.mainloop()

Now if you run the code, you will see a small window appearing on the screen as shown below.


But a small window is not fun. Right? So let us make this window to adapt the resolution of your computer screen. For that, we will have to get the length and height of your computer screen. This can be done using the method below.

screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()

The above methods will return the screen width and screen height respectively. Now, we have to use these variables to make the window size larger. That can be done using the following method.

window.geometry(f'{screen_width}x{screen_height}')

Now if you run the code, You will see the window of your computer screen size.

Now, let's add some buttons to it. We will be adding three buttons.

1. Import - To import an image from your computer for editing

2. Save - This button will save the edited image as a new file

3. Close - This will close the window.

We want to group these buttons for proper alignment and placing in our GUI. For that, we will be using Frames to group these buttons together. Frame is like a container that is responsible for the grouping of another widget. Let us create a frame using tk.Frame() method.

Frame1 = tk.Frame(window, height=20, width=200)
Frame1.pack(anchor=tk.N)

The method takes several arguments. We will be only using important ones. The first parameter is the window name on which you want to place the frame, The second and third parameters are the height and width of the frame.

The pack() method is used to place any widget on the window. The parameter anchor=tk.N signifies that the frame will be placed on the top side of the window.

Now, let us create the buttons that will be placed inside this frame. We will be using tk.Button() method for this.

importButton = tk.Button(Frame1, text="Import", padx=10, pady=5, command=importButton_callback)
importButton.grid(row=0, column=0)

saveButton = tk.Button(Frame1, text="Save", padx=10, pady=5, command=saveButton_callback)
saveButton.grid(row=0, column=1)

closeButton = tk.Button(Frame1, text="Close", padx=10, pady=5, command=closeButton_callback)
closeButton.grid(row=0, column=2)

The meaning of all the parameters is as follows.

1. Frame1:- The frame on which you want to place the button(you can use window name if you do not want to create frame).

2. text:- The label that will appear on the button.

3. padx:- Additional padding to the left and right of the text.

4. pady:- Additional padding above and below the text.

5. command:- The method that will be executed when the button is pressed.

Similar to pack() method, grid() method is also used to place a widget on the window. The grid method divides the space into rows and columns. So as per the code above the buttons are placed side by side.

Remember to define the methods that you have called in the command parameter or you will get an error.

def importButton_callback():
pass

def saveButton_callback():
pass

def closeButton_callback():
window.destroy()

Currently, we will not implement anything in these methods. We will add functionalities to these buttons later. For the close button we have called the method as window.destroy(). This will close the window. After running this code successfully the GUI will appear as shown below.




Happy 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