Image Filters with GUI

 avatar
unknown
python
4 months ago
10 kB
2
Indexable
import tkinter as tk
from tkinter import filedialog, Button, Canvas
from PIL import Image, ImageTk
import numpy as np


class ImageEditor:
    def __init__(self, root):
        self.root = root
        self.root.title("Image Filters with GUI")
        self.image_path = None
        self.image = None
        self.original_image = None  
        
        self.canvas = Canvas(root, width=200, height=200, bg="gray")
        self.canvas.pack()

        Button(root, text="Open Image", command=self.open_image).pack(side="left")
        Button(root, text="Negative", command=self.apply_negative).pack(side="left")
        Button(root, text="Decrease Brightness", command=self.decrease_brightness).pack(side="left")
        Button(root, text="Increase Brightness", command=self.increase_brightness).pack(side="left")
        Button(root, text="Power", command=self.apply_power).pack(side="left")
        Button(root, text="Log Transformation", command=self.apply_log).pack(side="left")
        Button(root, text="Average Filter", command=self.apply_average).pack(side="left")
        Button(root, text="Max Filter", command=self.apply_max).pack(side="left")
        Button(root, text="Min Filter", command=self.apply_min).pack(side="left")
        Button(root, text="Median Filter", command=self.apply_median).pack(side="left")
        Button(root, text="Sobel", command=self.apply_sobel).pack(side="left")
        Button(root, text="Delete Image", command=self.delete_image).pack(side="left")
        Button(root, text="Save Image", command=self.save_image).pack(side="left")
        Button(root, text="Remove Filter", command=self.remove_filter).pack(side="left")
        
    def open_image(self):
        self.image_path = filedialog.askopenfilename()
        if self.image_path:
            self.original_image = Image.open(self.image_path).convert('L')  
            self.image = self.original_image.copy() 
            self.display_image(self.image)


    def display_image(self, img):
        img = img.resize((200, 200)) 
        self.tk_image = ImageTk.PhotoImage(img)
        self.canvas.create_image(0, 0, anchor="nw", image=self.tk_image)

   
    def apply_negative(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.uint8)
            img_array = 255 - img_array 
            self.image = Image.fromarray(img_array)
            self.display_image(self.image)

    def decrease_brightness(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.uint8)
            img_array = img_array 
            img_array = np.clip(img_array, 0, 255)  
            self.image = Image.fromarray(img_array.astype(np.uint8))
            self.display_image(self.image)

    def increase_brightness(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.uint8)
            img_array = img_array * 2  
            img_array = np.clip(img_array, 0, 255)  
            self.image = Image.fromarray(img_array.astype(np.uint8))
            self.display_image(self.image)
    def apply_power(self, gamma=2.0):
        if self.image:
           img_array = np.array(self.image, dtype=np.float32)
           img_array = ((img_array/255)**gamma)*255
           img_array = np.clip(img_array, 0, 255).astype(np.uint8)
           self.image = Image.fromarray(img_array)
           self.display_image(self.image)
   
    def apply_log(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.float32)  
            img_array = np.where(img_array > 0, 30 * np.log2(img_array), 0)  
            self.image = Image.fromarray(np.clip(img_array, 0, 255).astype(np.uint8)) 
            self.display_image(self.image)


    def apply_average(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.float32)
            height, width = img_array.shape
            new_image = np.zeros_like(img_array)

            for row in range(1, height - 1):
                for col in range(1, width - 1):
                    new_image[row, col] = (
                        img_array[row, col] * (1/9) +
                        img_array[row-1, col] * (1/9) +
                        img_array[row+1, col] * (1/9) +
                        img_array[row, col-1] * (1/9) +
                        img_array[row, col+1] * (1/9) +
                        img_array[row-1, col-1] * (1/9) +
                        img_array[row-1, col+1] * (1/9) +
                        img_array[row+1, col-1] * (1/9) +
                        img_array[row+1, col+1] * (1/9)
                    )
            self.image = Image.fromarray(np.clip(new_image, 0, 255).astype(np.uint8))
            self.display_image(self.image)

   
    def apply_max(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.uint8)
            height, width = img_array.shape
            new_image = np.zeros_like(img_array)

            for row in range(1, height - 1):
                for col in range(1, width - 1):
                    neighbors = [
                        img_array[row, col], img_array[row-1, col],
                        img_array[row-1, col+1], img_array[row+1, col],
                        img_array[row+1, col+1], img_array[row+1, col-1],
                        img_array[row-1, col-1], img_array[row][col+1],
                        img_array[row][col-1]
                    ]
                    new_image[row, col] = np.max(neighbors)
            self.image = Image.fromarray(new_image)
            self.display_image(self.image)

    def apply_min(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.uint8)
            height, width = img_array.shape
            new_image = np.zeros_like(img_array)

            for row in range(1, height - 1):
                for col in range(1, width - 1):
                    neighbors = [
                        img_array[row, col], img_array[row-1, col],
                        img_array[row-1, col+1], img_array[row+1, col],
                        img_array[row+1, col+1], img_array[row+1, col-1],
                        img_array[row-1, col-1], img_array[row][col+1],
                        img_array[row][col-1]
                    ]
                    new_image[row, col] = np.min(neighbors)
            self.image = Image.fromarray(new_image)
            self.display_image(self.image)

   
    def apply_median(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.uint8)
            height, width = img_array.shape
            new_image = np.zeros_like(img_array)

            for row in range(1, height - 1):
                for col in range(1, width - 1):
                    neighbors = [
                        img_array[row, col], img_array[row-1, col],
                        img_array[row-1, col+1], img_array[row+1, col],
                        img_array[row+1, col+1], img_array[row+1, col-1],
                        img_array[row-1, col-1], img_array[row][col+1],
                        img_array[row][col-1]
                    ]
                    new_image[row, col] = np.median(neighbors)  
            self.image = Image.fromarray(new_image)
            self.display_image(self.image)

   
    def apply_sobel(self):
        if self.image:
            img_array = np.array(self.image, dtype=np.uint8) 
            gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) 
            gy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) 
            
            h, w = img_array.shape
            new = np.zeros_like(img_array)

            for row in range(1, h - 1):
                for col in range(1, w - 1):
                    Gx = (
                        img_array[row-1][col-1] * gx[0][0] + 
                        img_array[row-1][col] * gx[0][1] + 
                        img_array[row-1][col+1] * gx[0][2] +
                        img_array[row][col-1] * gx[1][0] + 
                        img_array[row][col] * gx[1][1] + 
                        img_array[row][col+1] * gx[1][2] +
                        img_array[row+1][col-1] * gx[2][0] + 
                        img_array[row+1][col] * gx[2][1] + 
                        img_array[row+1][col+1] * gx[2][2]
                    )
                    Gy = (
                        img_array[row-1][col-1] * gy[0][0] + 
                        img_array[row-1][col] * gy[0][1] + 
                        img_array[row-1][col+1] * gy[0][2] +
                        img_array[row][col-1] * gy[1][0] + 
                        img_array[row][col] * gy[1][1] + 
                        img_array[row][col+1] * gy[1][2] +
                        img_array[row+1][col-1] * gy[2][0] + 
                        img_array[row+1][col] * gy[2][1] + 
                        img_array[row+1][col+1] * gy[2][2]
                    )
                    
                    G = np.sqrt(Gx**2 + Gy**2) 
                    new[row, col] = np.clip(G, 0, 255) 
            
            self.image = Image.fromarray(new.astype(np.uint8)) 
            self.display_image(self.image)


    def remove_filter(self):
        if self.original_image:
            self.image = self.original_image.copy()
            self.display_image(self.image)

    
    def delete_image(self):
        self.image = None 
        self.canvas.delete("all") 


    def save_image(self):
        if self.image:
            save_path = filedialog.asksaveasfilename(defaultextension=".png",
                                                     filetypes=[("PNG files", "*.png"),
                                                                ("JPEG files", "*.jpg"),
                                                                ("All files", "*.*")])
            if save_path:
                self.image.save(save_path)
                print("Image saved to", save_path)

if __name__ == "__main__":
    root = tk.Tk()
    editor = ImageEditor(root)
    root.mainloop()
Editor is loading...
Leave a Comment