|
| 1 | +# Importing the required modules |
| 2 | +import tkinter as tk # graphical user interface toolkit |
| 3 | +from tkinter import * |
| 4 | +import easygui # to open the filebox |
| 5 | +import cv2 # for image processing |
| 6 | +import matplotlib.pyplot as plt |
| 7 | + |
| 8 | +import os # to read and save path |
| 9 | +import sys # |
| 10 | + |
| 11 | +# Making the GUI main window |
| 12 | +top = tk.Tk() |
| 13 | +top.geometry('400x400') |
| 14 | +top.title('Cartoonify Your Image !') |
| 15 | +top.configure(background='blue') |
| 16 | +label = Label(top, background='#CDCDCD', font=('calibri', 20, 'bold')) |
| 17 | + |
| 18 | +""" fileopenbox opens the box to choose file |
| 19 | +and help us store file path as string""" |
| 20 | + |
| 21 | + |
| 22 | +def upload(): |
| 23 | + image_path = easygui.fileopenbox() |
| 24 | + cartoonify(image_path) |
| 25 | + |
| 26 | + |
| 27 | +# Step 3: How is an image stored? |
| 28 | +def cartoonify(image_path): |
| 29 | + # read image |
| 30 | + original_image = cv2.imread(image_path) |
| 31 | + original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) |
| 32 | + print(original_image) # this will be stored in form of number |
| 33 | + |
| 34 | + # to confirm it is image that was chosing |
| 35 | + if original_image is None: |
| 36 | + print("Can't find any image. Choose appropriate file") |
| 37 | + sys.exit() |
| 38 | + resize_image1 = cv2.resize(original_image, (960, 540)) |
| 39 | + # plt.imshow(resize_image1, cmap='gray') |
| 40 | + |
| 41 | + # converting an image to grayscale |
| 42 | + grayscale_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY) |
| 43 | + resize_image2 = cv2.resize(grayscale_image, (960, 540)) |
| 44 | + # plt.imshow(resize_image2, cmap="gray") |
| 45 | + |
| 46 | + # applying median blur to smoothen an image |
| 47 | + smooth_grayscale_image = cv2.medianBlur(grayscale_image, 5) |
| 48 | + resize_image3 = cv2.resize(smooth_grayscale_image, (960, 540)) |
| 49 | + # plt.imshow(resize_image3, cmap='gray') |
| 50 | + |
| 51 | + # retrieving the edges for cartoon effect |
| 52 | + # by using thresholding technique |
| 53 | + get_edge = cv2.adaptiveThreshold(smooth_grayscale_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9) |
| 54 | + resize_image4 = cv2.resize(get_edge, (960, 540)) |
| 55 | + # plt.imshow(resize_image4, cmap='gray') |
| 56 | + |
| 57 | + # applying bilateral filter to remove noise |
| 58 | + # and keep edge sharp as required |
| 59 | + color_image = cv2.bilateralFilter(original_image, 9, 300, 300) |
| 60 | + resize_image5 = cv2.resize(color_image, (960, 540)) |
| 61 | + # plt.imshow(resize_image5, cmap="gray") |
| 62 | + |
| 63 | + # masking edged image with our "BEAUTIFY" image |
| 64 | + cartoon_image = cv2.bitwise_and(color_image, color_image, mask=get_edge) |
| 65 | + resize_image6 = cv2.resize(cartoon_image, (960, 540)) |
| 66 | + # plt.imshow(resize_image6, cmap='gray') |
| 67 | + |
| 68 | + # Plotting the whole transition |
| 69 | + images = [resize_image1, resize_image2, resize_image3, resize_image4, resize_image5, resize_image6] |
| 70 | + fig, axes = plt.subplots(3, 2, figsize=(8, 8), subplot_kw={'xticks': [], 'yticks': []}, |
| 71 | + gridspec_kw=dict(hspace=0.1, wspace=0.1)) |
| 72 | + for i, ax in enumerate(axes.flat): |
| 73 | + ax.imshow(images[i], cmap='gray') |
| 74 | + |
| 75 | + # Making a Save button in the GUI main window |
| 76 | + savel = Button(top, text="Save cartoon image", command=lambda: save(resize_image6, image_path), padx=30, pady=5) |
| 77 | + savel.configure(background='#364156', foreground='white', font=('calibri', 10, 'bold')) |
| 78 | + savel.pack(side=TOP, pady=50) |
| 79 | + |
| 80 | + # save button code |
| 81 | + plt.show() |
| 82 | + |
| 83 | + |
| 84 | +def save(resize_image6, image_path): |
| 85 | + # saving an image using imwrite function |
| 86 | + new_name = "cartoonified_Image" |
| 87 | + path1 = os.path.dirname(image_path) |
| 88 | + extension = os.path.splitext(image_path)[1] |
| 89 | + path = os.path.join(path1, new_name + extension) |
| 90 | + cv2.imwrite(path, cv2.cvtColor(resize_image6, cv2.COLOR_RGB2BGR)) |
| 91 | + I = "Image saved by name " + new_name + " at " + path |
| 92 | + tk.messagebox.showinfo(title=None, message=I) |
| 93 | + |
| 94 | + |
| 95 | +# Making the Cartoonify button in the GUI main window |
| 96 | +upload = Button(top, text="Cartoonify an Image", command=upload, padx=10, pady=5) |
| 97 | +upload.configure(background="#374256", foreground="wheat", font=('calibri', 10, 'bold')) |
| 98 | +upload.pack(side=TOP, pady=50) |
| 99 | + |
| 100 | +# Main function to build the GUI window |
| 101 | +top.mainloop() |
| 102 | +{"mode":"full","isActive":false} |
0 commit comments