How to Make an Image Editor in Python | पाइथन से इमेज एडिटर कैसे बनाये ?

IMAGE EDITOR SOFTWARE BY USING PYTHON AND SOME MODULE(" tkinter , pil "):

reload ! diplomawale.in


CODE DESCRIPTION:

This code is a simple image editor application built using the Tkinter library for the graphical user interface (GUI) and the Python Imaging Library (PIL, now known as Pillow) for image processing. The application allows you to open an image file (PNG, JPG, or JPEG) and apply various image editing operations to it, such as flipping, rotating, inverting colors, blurring, increasing brightness, and converting to grayscale. Here's a detailed explanation of the code:

Importing Required Libraries:

    • The code starts by importing the necessary libraries:
      • Tk from tkinter for creating the GUI.
      • file dialog from tkinter to open a file dialog for selecting an image.
      • Various functions and classes from PIL (Pillow) for image manipulation.

ImageEditorApp Class:

    • This class represents the main application window.
    • The constructor __init__ initializes the GUI elements and sets the window title.

GUI Elements:

    • image_label: A label widget to display the image.
    • open_button: A button to open an image file.
    • options: A list of tuples, each containing an operation name and its corresponding function. These options are displayed as buttons in the GUI.

open_image Method:

    • This method is called when the "Open Image" button is clicked.
    • It uses filedialog.askopenfilename to open a file dialog and get the selected image file's path.
    • If a file is selected, it opens the image using PIL (Image.open) and displays it on the GUI using display_image.

display_image Method:

    • This method takes an image as input and displays it on the GUI using ImageTk.PhotoImage and the config method of image_label.

Operation Methods:

    • There are several methods for image manipulation, such as flip_horizontal, rotate_90, invert_colors, apply_blur, increase_brightness, and convert_to_greyscale.
    • Each method checks if the original_image attribute exists (i.e., if an image is opened) and then applies the corresponding image processing operation to the original image.
    • The modified image is displayed using the apply_modification method, which, in turn, calls display_image.

__main__ Block:

    • This block is executed when the script is run directly (not imported as a module).
    • It creates a Tkinter window, initializes the ImageEditorApp class, and enters the main event loop using root.mainloop().

 CLICK HERE TO DOWNLOAD SOURCE CODE AND OUTPUT IMAGE

SOURCE CODE:



from tkinter import Tk, filedialog, Label, Button, Menu, StringVar, OptionMenu
from PIL import Image, ImageTk, ImageOps, ImageFilter, ImageEnhance, ImageChops


class ImageEditorApp:
def __init__(self, root):
self.root = root
self.root.title("Image Editor by Gaurav")

self.image_label = Label(root)
self.image_label.pack(padx=20, pady=20)

open_button = Button(root, text="Open Image", command=self.open_image)
open_button.pack(padx=10, pady=5)

adjustment_options = [
"Select Adjustment", "Brightness", "Contrast", "Saturation"
]

filter_effect_options = [
"Select Filter/Effect", "Blur", "Sharpen", "Edge Enhance",
             "Sepia", "Grayscale", "Invert Colors"
]

# Create a variable to hold the selected adjustment tool
self.selected_adjustment = StringVar()
self.selected_adjustment.set(adjustment_options[0])

# Create a variable to hold the selected filter/effect
self.selected_filter_effect = StringVar()
self.selected_filter_effect.set(filter_effect_options[0])

# Create dropdown menus for adjustment tools and filter/effects
adjustment_menu = OptionMenu(root, self.selected_adjustment, *adjustment_options)
adjustment_menu.pack(padx=10, pady=5)

filter_effect_menu = OptionMenu(root, self.selected_filter_effect,
                     *filter_effect_options)
filter_effect_menu.pack(padx=10, pady=5)

apply_adjustment_button = Button(root, text="Apply Adjustment",
                      command=self.apply_adjustment)
apply_adjustment_button.pack(padx=10, pady=5)

apply_filter_effect_button = Button(root, text="Apply Filter/Effect",
                    command=self.apply_filter_effect)
apply_filter_effect_button.pack(padx=10, pady=5)

options = [
("Flip Horizontal", self.flip_horizontal),
("Rotate 90°", self.rotate_90),
("Custom Function", self.custom_function),
("Save Image", self.save_image),
("Crop", self.crop_image),
("Resize", self.resize_image)
]

for option_text, option_function in options:
option_button = Button(root, text=option_text, command=option_function)
option_button.pack(padx=10, pady=5)

self.original_image = None
self.edited_image = None

def open_image(self):
file_path = filedialog.askopenfilename(filetypes=
                [("Image Files", "*.png;*.jpg;*.jpeg")])
if file_path:
self.original_image = Image.open(file_path)
            # Create a copy to track edits
self.edited_image = self.original_image.copy()
self.display_image(self.edited_image)

def display_image(self, image):
self.tk_image = ImageTk.PhotoImage(image)
self.image_label.config(image=self.tk_image)

def apply_modification(self, modified_image):
self.edited_image = modified_image # Update the edited image
self.display_image(modified_image)

def flip_horizontal(self):
if hasattr(self, 'edited_image'):
modified_image = self.edited_image.transpose(Image.FLIP_LEFT_RIGHT)
self.apply_modification(modified_image)

def rotate_90(self):
if hasattr(self, 'edited_image'):
modified_image = self.edited_image.transpose(Image.ROTATE_90)
self.apply_modification(modified_image)

def custom_function(self):
if hasattr(self, 'edited_image'):
# Implement your custom image editing function here
modified_image = self.edited_image # Placeholder
self.apply_modification(modified_image)

def save_image(self):
if self.edited_image:
file_path = filedialog.asksaveasfilename(defaultextension=".png",
filetypes=[("PNG files", "*.png"),
                                                                ("JPEG files", "*.jpg"),
("All files", "*.*")])
if file_path:
self.edited_image.save(file_path)
print(f"Image saved as {file_path}")

def crop_image(self):
if self.original_image:
# Create a temporary edited image from the original
edited_image = self.original_image.copy()

# Open a cropping tool dialog (you can implement a custom dialog here)
# For simplicity, let's assume we manually specify the cropping coordinates.
left = 100 # Left edge of the cropping area
upper = 100 # Upper edge of the cropping area
right = 300 # Right edge of the cropping area
lower = 300 # Lower edge of the cropping area

# Crop the image based on the specified coordinates
edited_image = edited_image.crop((left, upper, right, lower))

# Apply the modification and display the cropped image
self.apply_modification(edited_image)
self.edited_image = edited_image

def resize_image(self):
if self.original_image:
# Create a temporary edited image from the original
edited_image = self.original_image.copy()

# Open a resize tool dialog (you can implement a custom dialog here)
# For simplicity, let's assume we manually specify the new dimensions.
new_width = 400
new_height = 300

# Resize the image based on the specified dimensions
edited_image = edited_image.resize((new_width, new_height))

# Apply the modification and display the resized image
self.apply_modification(edited_image)
self.edited_image = edited_image

def apply_adjustment(self):
selected_tool = self.selected_adjustment.get()
if selected_tool == "Brightness":
self.adjust_brightness()
elif selected_tool == "Contrast":
self.adjust_contrast()
elif selected_tool == "Saturation":
self.adjust_saturation()

def apply_filter_effect(self):
selected_effect = self.selected_filter_effect.get()
if selected_effect == "Blur":
self.apply_blur()
elif selected_effect == "Sharpen":
self.apply_sharpen()
elif selected_effect == "Edge Enhance":
self.apply_edge_enhance()
elif selected_effect == "Sepia":
self.apply_sepia()
elif selected_effect == "Grayscale":
self.apply_grayscale()
elif selected_effect == "Invert Colors":
self.apply_invert_colors()

def adjust_brightness(self):
if hasattr(self, 'edited_image'):
enhancer = ImageEnhance.Brightness(self.edited_image)
            # Increase brightness by a factor of 1.5
modified_image = enhancer.enhance(1.5)
self.apply_modification(modified_image)

def adjust_contrast(self):
if hasattr(self, 'edited_image'):
enhancer = ImageEnhance.Contrast(self.edited_image)
            # Increase contrast by a factor of 1.5
modified_image = enhancer.enhance(1.5)
self.apply_modification(modified_image)

def adjust_saturation(self):
if hasattr(self, 'edited_image'):
enhancer = ImageEnhance.Color(self.edited_image)
             # Increase saturation by a factor of 1.5
modified_image = enhancer.enhance(1.5)
self.apply_modification(modified_image)

def apply_blur(self):
if hasattr(self, 'edited_image'):
modified_image = self.edited_image.filter(ImageFilter.BLUR)
self.apply_modification(modified_image)

def apply_sharpen(self):
if hasattr(self, 'edited_image'):
modified_image = self.edited_image.filter(ImageFilter.SHARPEN)
self.apply_modification(modified_image)

def apply_edge_enhance(self):
if hasattr(self, 'edited_image'):
modified_image = self.edited_image.filter(ImageFilter.EDGE_ENHANCE)
self.apply_modification(modified_image)

def apply_sepia(self):
if hasattr(self, 'edited_image'):
# Applying a simple sepia filter
sepia_image = ImageOps.colorize(self.edited_image.convert("L"),
                "#704214", "#C0A080")
self.apply_modification(sepia_image)

def apply_grayscale(self):
if hasattr(self, 'edited_image'):
modified_image = self.edited_image.convert("L")
self.apply_modification(modified_image)

def apply_invert_colors(self):
if hasattr(self, 'edited_image'):
modified_image = ImageOps.invert(self.edited_image)
self.apply_modification(modified_image)


if __name__ == "__main__":
root = Tk()
app = ImageEditorApp(root)
root.mainloop()



OUTPUT:


How to Make an Image Editor in Python | पाइथन से इमेज एडिटर कैसे बनाये ? | diplomawale.in

How to Make an Image Editor in Python | पाइथन से इमेज एडिटर कैसे बनाये ? | diplomawale.in

How to Make an Image Editor in Python | पाइथन से इमेज एडिटर कैसे बनाये ? | diplomawale.in

How to Make an Image Editor in Python | पाइथन से इमेज एडिटर कैसे बनाये ? | diplomawale.in

How to Make an Image Editor in Python | पाइथन से इमेज एडिटर कैसे बनाये ? | diplomawale.in


Post a Comment

0 Comments