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
Post a Comment