Skip to main content

Image Editing Software | Importing And Saving Images

 Hello friends! Welcome back. This is going to be a single post where you will be learning about importing images, displaying images, and saving the images on the image editing GUI window.

So let's get started. We will continue to build our previous code. First, we will add the required library packages to our code. 

from PIL import Image
from PIL import ImageTk
from tkinter import filedialog

The Image and ImageTk library packages will be used for opening and displaying the image on the window. The filedialog library will be used for browsing the images from the computer directory that you want to import.

Let us now jump to writing the code. For importing a new image you will have to add the following code to the import button callback method.

def importButton_callback():
global originalImage
filename = filedialog.askopenfilename()
originalImage = Image.open(filename)
dispayImage(originalImage)

We have defined originalImage as a global variable. This is because we will be using this variable in all the other methods for editing the image. The filename variable holds the path of the image that you want to import. filedialog.askopenfile() method will open a new window and will allow you to browse your computer directory for the images that you want to import. Image.open() will open the image with a given file path. originalImage variable will hold the image in RGB format. displayImage() is the method we will write for displaying the image on the GUI window.

Let us now write the displayImage() method.

def dispayImage(displayImage):
ImagetoDisplay = displayImage.resize((900,600), Image.ANTIALIAS)
ImagetoDisplay = ImageTk.PhotoImage(ImagetoDisplay)
showWindow.config(image=ImagetoDisplay)
showWindow.photo_ref = ImagetoDisplay
showWindow.pack()

displayImage.resize resizes the original image into a given pixel size. Image.ANTIALIAS is the resizing algorithm used to resize the image. ImageTk.PhotoImage converts the resized image into Tkinter compatible photo image. For displaying the image on the GUI window we have to configure the window with the updated image using showWindow.config() method. You have to keep a photo reference using showWindow.photo_ref before displaying the photo on the window. Otherwise, the updated image won't appear.  And at last pack the window.

And now it's time for saving the image. Let us write saveButton_callback() method.

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

For saving the image you have to use filedialog.asksaveasfile() method. This method will open a new window where you will be asked to save the file and type a file name. You can also mention the default extension of the image. savefile will contain the path of the file where it has to be saved. And the last line will be to save the image using originalImage.save() method. This will save the file in your computer directory. 

That's it. 

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