0% found this document useful (0 votes)
6 views13 pages

Image Processing

The document describes a Python-based GUI application for image processing that allows users to apply filters such as grayscale, blur, sharpen, and edge detection using Tkinter, OpenCV, and Pillow. It features a user-friendly interface for opening, displaying, and saving images, along with future enhancement suggestions like adding more filters and batch processing capabilities. The app aims to simplify image editing for users without advanced technical knowledge.

Uploaded by

techstack901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Image Processing

The document describes a Python-based GUI application for image processing that allows users to apply filters such as grayscale, blur, sharpen, and edge detection using Tkinter, OpenCV, and Pillow. It features a user-friendly interface for opening, displaying, and saving images, along with future enhancement suggestions like adding more filters and batch processing capabilities. The app aims to simplify image editing for users without advanced technical knowledge.

Uploaded by

techstack901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Image Processing

INTRODUCTION

This project is a Python-based GUI application that allows users to apply various filters to
images. Built using Tkinter, OpenCV, and Pillow (PIL), this lightweight tool provides a user-
friendly interface to process and enhance images with a few clicks. It is designed to offer
essential image processing functionality without requiring complex software or advanced
technical knowledge.

Features

✅ Open & Display Images


Users can select and open image files in common formats like PNG, JPEG, and BMP. The
application uses the filedialog module from Tkinter to browse local directories and select files.
Once selected, the image is automatically displayed in the app window, resized to fit within the
interface dimensions while maintaining its aspect ratio.

✅ Apply Filters
The app provides four primary filters that users can apply to their images:

• Grayscale – Converts the image into shades of gray by removing color information. This
filter is useful for artistic effects or preparing images for further analysis. The app uses
cv2.cvtColor() with the COLOR_RGB2GRAY flag to achieve this effect.
• Blur – Applies a Gaussian blur to soften the image. This effect helps reduce noise and
details, making it suitable for backgrounds or artistic blurring. The app uses
cv2.GaussianBlur() with a kernel size of 15x15 for optimal balance between smoothness
and performance.
• Sharpen – Enhances image details using a custom convolution kernel. This filter makes
edges more defined and is commonly used to improve photo clarity. The sharpening
effect is applied using cv2.filter2D() with a predefined kernel matrix.
• Edge Detection – Highlights edges within the image using the Canny Edge Detection
algorithm. This filter is useful for detecting object boundaries and performing image
segmentation. The app applies cv2.Canny() with adjustable threshold values of 100 and
200.

✅ Save Processed Images


Users can save the edited images in PNG or JPEG formats using the
filedialog.asksaveasfilename() method. The app converts the processed image array back to the
Pillow Image format before saving to ensure compatibility with different file types. The default
file extension is set to PNG, but users can manually select other formats from the dialog box.

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 1


Image Processing

✅ User-Friendly GUI
The interface is built with Tkinter, providing an organized layout with interactive buttons for
each action. The app features:

• Custom button styles with a consistent color theme.


• Automatic image resizing for display.
• Clear separation between image display and control buttons.
• Error handling to prevent invalid operations when no image is loaded.

Additional Features for Future Improvement

🔹 Add More Filters


Additional filters like brightness adjustment, color inversion, and noise reduction could be
integrated to enhance the app's functionality and appeal to more advanced users.

🔹 Undo Functionality
Implementing an undo feature would allow users to revert the last applied filter without needing
to reload the original image.

🔹 Custom Kernel Filters


Allowing users to define their own convolution kernels would provide greater flexibility for
advanced image processing applications.

🔹 Batch Processing
The ability to apply filters to multiple images at once would improve efficiency for users
working with large sets of images.

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 2


Image Processing

IMPLEMENTATION

1. Setting Up the Environment


To build the Image Filter App, install the required dependencies:

pip install opencv-python numpy pillow tkinter

These libraries provide image processing capabilities, GUI components, and numerical
operations.

2. Building the GUI with Tkinter


The graphical interface is structured with Tkinter:
• Main Window: Created using Tk(), with a set title and dimensions.
• Frames: Used to organize the image display and control buttons.
• Labels: Display images in the application window.
• Buttons: Trigger different image operations and file actions.

Example snippet:

self.root = Tk()
self.root.title("Image Filter App")
self.label = Label(self.root)
self.label.pack()

3. Loading and Displaying Images


• The filedialog.askopenfilename() method allows users to select an image.
• cv2.imread() loads the image in BGR format, converted to RGB using cv2.cvtColor().
• The image is resized and displayed using Pillow (PIL).
file_path = filedialog.askopenfilename()
self.img_cv = cv2.imread(file_path)
self.img_cv = cv2.cvtColor(self.img_cv, cv2.COLOR_BGR2RGB)
self.display_image(self.img_cv)

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 3


Image Processing

4. Applying Filters
The app provides multiple image filters, each implemented using OpenCV.
✅ Grayscale Conversion
filtered = cv2.cvtColor(self.img_cv, cv2.COLOR_RGB2GRAY)
filtered = cv2.cvtColor(filtered, cv2.COLOR_GRAY2RGB)
Converts an image to grayscale and back to RGB for proper display.

✅ Blurring Effect
filtered = cv2.GaussianBlur(self.img_cv, (15, 15), 0)
Applies Gaussian blur with a 15x15 kernel to smooth the image.

✅ Sharpening Filter
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
filtered = cv2.filter2D(self.img_cv, -1, kernel)
Enhances the image by amplifying edge details.

✅ Edge Detection
filtered = cv2.Canny(self.img_cv, 100, 200)
filtered = cv2.cvtColor(filtered, cv2.COLOR_GRAY2RGB)
Detects edges using the Canny algorithm and converts it back to RGB.

5. Displaying Processed Images


The processed image is updated in the GUI using Pillow:
img = Image.fromarray(filtered)
img = img.resize((400, 300), Image.LANCZOS)
img_tk = ImageTk.PhotoImage(img)
self.label.config(image=img_tk)
self.label.image = img_tk

This ensures smooth scaling and proper rendering of the modified image.

6. Saving Images
Users can save processed images in PNG or JPEG format:
save_path = filedialog.asksaveasfilename(defaultextension=".png")
Image.fromarray(self.img_cv).save(save_path)
The image is converted from an array format back into a standard image format before
saving.

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 4


Image Processing

7. Error Handling & User Experience Enhancements


To prevent crashes or unintended behavior:
• A check ensures an image is loaded before applying filters.
• Error messages prompt users if an invalid action is attempted.
Example error handling:

if self.img_cv is None:
messagebox.showerror("Error", "No image loaded!")
return

8. Future Enhancements
• Implement Undo functionality using a stack of image states.
• Add Brightness & Contrast adjustments for greater control.
• Enable Batch Processing to apply filters to multiple images simultaneously.
This structured approach ensures a robust and user-friendly image filtering application with
expandable features for future improvements.

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 5


Image Processing

PROGRAM

import cv2
import numpy as np
from tkinter import Tk, Button, Label, filedialog, Frame
from PIL import Image, ImageTk

def open_image():
global img, img_cv
file_path = filedialog.askopenfilename()
if file_path:
img_cv = cv2.imread(file_path)
img_cv = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img_cv)
img = img.resize((400, 300))
img_tk = ImageTk.PhotoImage(img)
label.config(image=img_tk)
label.image = img_tk

def apply_filter(filter_type):
global img_cv
if img_cv is None:
return

if filter_type == "grayscale":
filtered = cv2.cvtColor(img_cv, cv2.COLOR_RGB2GRAY)
filtered = cv2.cvtColor(filtered, cv2.COLOR_GRAY2RGB)
elif filter_type == "blur":
filtered = cv2.GaussianBlur(img_cv, (15, 15), 0)
elif filter_type == "sharpen":
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
filtered = cv2.filter2D(img_cv, -1, kernel)
elif filter_type == "edge":
filtered = cv2.Canny(img_cv, 100, 200)
filtered = cv2.cvtColor(filtered, cv2.COLOR_GRAY2RGB)
else:
return

img_filtered = Image.fromarray(filtered)
img_filtered = img_filtered.resize((400, 300))
img_tk = ImageTk.PhotoImage(img_filtered)
label.config(image=img_tk)
label.image = img_tk

def save_image():

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 6


Image Processing

if img_cv is not None:


save_path =
filedialog.asksaveasfilename(defaultextension=".png",
filetypes=[("PNG
files", "*.png"), ("JPEG files", "*.jpg"),
("All
Files", "*.*")])
if save_path:
Image.fromarray(img_cv).save(save_path)

# GUI Setup
root = Tk()
root.title("Image Filter App")
root.configure(bg="#2E3B4E")
root.geometry("500x500")

frame = Frame(root, bg="#2E3B4E")


frame.pack(pady=10)

label = Label(frame, bg="#2E3B4E")


label.pack()

button_frame = Frame(root, bg="#2E3B4E")


button_frame.pack(pady=10)

button_style = {"bg": "#4CAF50", "fg": "white", "font": ("Arial", 12),


"padx": 10, "pady": 5, "borderwidth": 2,
"relief": "raised"}

Button(button_frame, text="Open Image", command=open_image,


**button_style).pack(pady=5)
Button(button_frame, text="Grayscale", command=lambda:
apply_filter("grayscale"), **button_style).pack(pady=5)
Button(button_frame, text="Blur", command=lambda: apply_filter("blur"),
**button_style).pack(pady=5)
Button(button_frame, text="Sharpen", command=lambda:
apply_filter("sharpen"), **button_style).pack(pady=5)
Button(button_frame, text="Edge Detection", command=lambda:
apply_filter("edge"), **button_style).pack(pady=5)
Button(button_frame, text="Save Image", command=save_image,
**button_style).pack(pady=5)

root.mainloop()

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 7


Image Processing

OUTPUT

Image Loading & Display


• Users can select image files using a file selection dialog.
• Supported formats: PNG, JPEG, BMP, TIFF
• Images are resized for display while maintaining aspect ratio.

Image Processing Filters


The following filters are available:
✅ Grayscale Conversion
• Converts the image to grayscale using OpenCV’s cv2.cvtColor() function.
✅ Gaussian Blur
• Applies a smoothing effect using cv2.GaussianBlur() with a 15x15 kernel.
✅ Sharpening
• Uses a convolution kernel to enhance details via cv2.filter2D().
✅ Edge Detection
• Detects image edges using the Canny algorithm (cv2.Canny()).

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 8


Image Processing

2.3 Saving Processed Images


• Users can save edited images in PNG, JPG formats.
• The file selection dialog provides format options before saving.

Expected Output
Action Expected Result
Load Image Selected image appears in the GUI.
Apply Grayscale Image is converted to grayscale.
Apply Blur Image becomes softer and less detailed.
Apply Sharpening Image details and edges become more pronounced.
Apply Edge Detect Outlines of objects in the image become visible.
Save Image Processed image is saved in the selected format.

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 9


Image Processing

CONCLUSION

The Image Filter App successfully demonstrates how image processing can be simplified
through an interactive GUI using Python, OpenCV, and Tkinter. By allowing users to open,
modify, and save images with just a few clicks, the app provides a practical and user-friendly
approach to basic image editing.

With essential filters such as grayscale conversion, blurring, sharpening, and edge
detection, users can enhance and analyze images efficiently. The structured implementation
ensures smooth functionality, while error handling prevents unexpected issues.

This project serves as a solid foundation for further expansion. Future improvements such as
undo functionality, advanced filters, batch processing, and custom kernel support can
significantly enhance the app’s capabilities. Overall, the Image Filter App is an excellent
tool for beginners and a stepping stone toward more advanced image processing applications.

Conclusion

The Image Filter App successfully demonstrates how fundamental image processing
operations can be implemented through an interactive and user-friendly Graphical User
Interface (GUI) using Python, OpenCV, Tkinter, and Pillow. By allowing users to open,
apply filters, and save images with minimal effort, the application eliminates the need for
complex image editing software while maintaining essential functionalities.

The app incorporates four key image processing filters:


✅ Grayscale Conversion – Converts the image into shades of gray, which is commonly
used for artistic, scientific, and analytical purposes.

✅ Gaussian Blur – Smooths the image to reduce noise, providing a soft-focus effect useful
for backgrounds or pre-processing before edge detection.

✅ Sharpening – Enhances fine details and increases image clarity, making it valuable for
improving object visibility.

✅ Edge Detection – Identifies significant structural details in the image, often used in
computer vision applications such as object recognition and segmentation.

With a well-structured GUI, the app ensures an intuitive experience where users can
seamlessly navigate through different functionalities. The Tkinter-based layout organizes
the controls efficiently, and automatic image resizing enhances visibility. Additionally, built-
in error handling mechanisms prevent users from performing invalid operations, ensuring a
smooth user experience.

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 10


Image Processing

This project serves as a strong foundation for further development in image processing.
Potential enhancements include:

🔹 Undo/Redo Functionality – Enabling users to revert or reapply previous filters without


reloading the original image.

🔹 Advanced Image Adjustments – Incorporating contrast, brightness, and noise


reduction features for better control over image quality.

🔹 Batch Processing – Allowing users to apply filters to multiple images at once,


improving efficiency in bulk image modifications.

🔹 Custom Kernel Processing – Enabling users to define and apply their own convolution
filters for advanced image effects.

🔹 Real-time Camera Feed Processing – Integrating webcam support to process images in


real time using the same filtering techniques.

Overall, the Image Filter App is a compact yet powerful tool that bridges the gap between
beginner-friendly applications and professional image processing techniques. It provides an
excellent starting point for those interested in exploring computer vision, GUI development,
and digital image manipulation, making it an ideal project for both learning and practical use.

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 11


Image Processing

❖ References
1) www.google.com
2) www.javatpoint.com
3) For type this project we have use a MS-word
4) And we have completed the projects under guidance of Miss. Raskar. P.B

`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 12


`A.C.S.’S. DIPLOMA IN ENGG & TECH, ASHTI Page | 13

You might also like