Code Explanation
Step 1: Importing Libraries
In first necessary libraries are imported. tkinter
provides GUI functionality, filedialog
and messagebox
modules handle file dialogs and message boxes, respectively. PIL
(Pillow) is used for image processing, and shutil
and os
assist in managing files and directories.
Python3
import tkinter as tk from tkinter import filedialog, messagebox from PIL import Image, ImageTk import shutil import os |
Step 2: Defining the Upload Function
beloa code defines the upload_image
function, utilizing filedialog
to prompt the user to select an image file. It then opens and resizes the image using Pillow, updates the Tkinter label to display the image, and calls the save_image
function to store the image. A success message is displayed using messagebox
.
Python3
def upload_image(): file_path = filedialog.askopenfilename() if file_path: image = Image. open (file_path) image.thumbnail(( 300 , 300 )) # Resize image if necessary photo = ImageTk.PhotoImage(image) image_label.config(image = photo) image_label.image = photo # Keep a reference to avoid garbage collection save_image(file_path) messagebox.showinfo( "Success" , "Image uploaded successfully!" ) |
Step 3: Image Saving Function
below code part defines save_image
, responsible for creating a directory (saved_images
) if it doesn’t exist, extracting the filename from the provided path, and copying the image to the specified directory. A message is printed indicating the successful saving of the image.
Python3
def save_image(file_path): save_dir = "saved_images" if not os.path.exists(save_dir): os.makedirs(save_dir) filename = os.path.basename(file_path) shutil.copy(file_path, os.path.join(save_dir, filename)) print ( "Image saved to:" , os.path.join(save_dir, filename)) |
Step 4: Creating the Tkinter Window
In the pbelow code art, the Tkinter window is created with the title “Image Uploader” and a size of 500×500 pixels.
Python3
root = tk.Tk() root.title( "Image Uploader" ) root.geometry( "500x500" ) |
Step 5: Create and Pack Widgets, Running the Application
The last part involves creating Tkinter widgets – a button (upload_button
) and a label (image_label
). The button triggers the upload_image
function when clicked, and both widgets are packed into the window. The mainloop()
method is called to run the Tkinter application, allowing user interaction with the image uploader interface.
Python3
upload_button = tk.Button(root, text = "Upload Image" , command = upload_image) upload_button.pack(pady = 10 ) image_label = tk.Label(root) image_label.pack() root.mainloop() |
Complete Code
Python3
import tkinter as tk from tkinter import filedialog, messagebox from PIL import Image, ImageTk import shutil import os # Define the function to upload and save the image def upload_image(): file_path = filedialog.askopenfilename() if file_path: image = Image. open (file_path) image.thumbnail(( 300 , 300 )) # Resize image if necessary photo = ImageTk.PhotoImage(image) image_label.config(image = photo) image_label.image = photo # Keep a reference to avoid garbage collection save_image(file_path) messagebox.showinfo( "Success" , "Image uploaded successfully!" ) def save_image(file_path): save_dir = "saved_images" if not os.path.exists(save_dir): os.makedirs(save_dir) filename = os.path.basename(file_path) shutil.copy(file_path, os.path.join(save_dir, filename)) print ( "Image saved to:" , os.path.join(save_dir, filename)) # Create the main window root = tk.Tk() root.title( "Image Uploader" ) # Set window size root.geometry( "500x500" ) # Create and pack widgets upload_button = tk.Button(root, text = "Upload Image" , command = upload_image) upload_button.pack(pady = 10 ) image_label = tk.Label(root) image_label.pack() # Run the application root.mainloop() |
Run the server
run the server using below command
python script_name.py
Output
Save Image To File in Python using Tkinter
Saving an uploaded image to a local directory using Tkinter combines the graphical user interface capabilities of Tkinter with the functionality of handling and storing images in Python. In this article, we will explore the steps involved in achieving this task, leveraging Tkinter’s GUI features to enhance the user experience in image management applications.
What is Tkinter?
Tkinter is a standard GUI (Graphical User Interface) toolkit for Python, providing a set of modules and libraries to create interactive and visually appealing desktop applications. Tkinter is based on the Tk GUI toolkit, and it comes bundled with most Python installations, making it readily available for developers. With Tkinter, developers can design windows, dialogs, buttons, and other GUI elements, facilitating the development of user-friendly applications with ease