|
| 1 | + |
| 2 | +# Image Snipper |
| 3 | + |
| 4 | +# imported necessary library |
| 5 | +import tkinter |
| 6 | +from tkinter import * |
| 7 | +import tkinter as tk |
| 8 | +import tkinter.messagebox as mbox |
| 9 | +from tkinter import ttk |
| 10 | +from tkinter import filedialog |
| 11 | +from PIL import ImageTk, Image |
| 12 | +import cv2 |
| 13 | +import numpy as np |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +# Main Window & Configuration |
| 18 | +window = tk.Tk() # created a tkinter gui window frame |
| 19 | +window.title("Image Snipper") # title given is "DICTIONARY" |
| 20 | +window.geometry('1000x700') |
| 21 | + |
| 22 | +# top label |
| 23 | +start1 = tk.Label(text = "IMAGE SNIPPER", font=("Arial", 50), fg="magenta") # same way bg |
| 24 | +start1.place(x = 220, y = 10) |
| 25 | + |
| 26 | +def start_fun(): |
| 27 | + window.destroy() |
| 28 | + |
| 29 | +# start button created |
| 30 | +startb = Button(window, text="START",command=start_fun,font=("Arial", 25), bg = "orange", fg = "blue", borderwidth=3, relief="raised") |
| 31 | +startb.place(x =100 , y =600 ) |
| 32 | + |
| 33 | +# image on the main window |
| 34 | +path = "Images/front.jpg" |
| 35 | +# Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object. |
| 36 | +img1 = ImageTk.PhotoImage(Image.open(path)) |
| 37 | +# The Label widget is a standard Tkinter widget used to display a text or image on the screen. |
| 38 | +panel = tk.Label(window, image = img1) |
| 39 | +panel.place(x = 150, y = 90) |
| 40 | + |
| 41 | +# function created for exiting |
| 42 | +def exit_win(): |
| 43 | + if mbox.askokcancel("Exit", "Do you want to exit?"): |
| 44 | + window.destroy() |
| 45 | + |
| 46 | +# exit button created |
| 47 | +exitb = Button(window, text="EXIT",command=exit_win,font=("Arial", 25), bg = "red", fg = "blue", borderwidth=3, relief="raised") |
| 48 | +exitb.place(x =800 , y =600 ) |
| 49 | +window.protocol("WM_DELETE_WINDOW", exit_win) |
| 50 | +window.mainloop() |
| 51 | + |
| 52 | +click1 = False |
| 53 | +point1 = (0, 0) |
| 54 | +def click(event, x, y, flags, params): |
| 55 | + global click1, point1,img |
| 56 | + if event == cv2.EVENT_LBUTTONDOWN: |
| 57 | + # if mousedown, store the x,y position of the mous |
| 58 | + click1 = True |
| 59 | + point1 = (x, y) |
| 60 | + elif event == cv2.EVENT_MOUSEMOVE and click1: |
| 61 | + # when dragging pressed, draw rectangle in image |
| 62 | + img_copy = img.copy() |
| 63 | + cv2.rectangle(img_copy, point1, (x, y), (0, 0, 255), 2) |
| 64 | + cv2.imshow("Image", img_copy) |
| 65 | + elif event == cv2.EVENT_LBUTTONUP: |
| 66 | + # on mouseUp, create subimage |
| 67 | + click1 = False |
| 68 | + sub_img = img[point1[1]:y, point1[0]:x] |
| 69 | + cv2.imshow("Snipped Image", sub_img) |
| 70 | + |
| 71 | + |
| 72 | +# Main Window & Configuration |
| 73 | +window1 = tk.Tk() # created a tkinter gui window frame |
| 74 | +window1.title("Image Snipper") # title given is "DICTIONARY" |
| 75 | +window1.geometry('1000x700') |
| 76 | + |
| 77 | +# top label |
| 78 | +start1 = tk.Label(text = "IMAGE SNIPPER", font=("Arial", 50), fg="magenta") # same way bg |
| 79 | +start1.place(x = 220, y = 10) |
| 80 | + |
| 81 | +# image on the main window |
| 82 | +path = "Images/second.jpg" |
| 83 | +# Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object. |
| 84 | +img1 = ImageTk.PhotoImage(Image.open(path)) |
| 85 | +# The Label widget is a standard Tkinter widget used to display a text or image on the screen. |
| 86 | +panel = tk.Label(window1, image = img1) |
| 87 | +panel.place(x = 350, y = 100) |
| 88 | + |
| 89 | +# top label |
| 90 | +sec1 = tk.Label(text = "Select any image &\nsnip some part of it...", font=("Arial", 40), fg="green") # same way bg |
| 91 | +sec1.place(x = 250, y = 350) |
| 92 | + |
| 93 | +# lbl1 = tk.Label(text="File Path : ",font=("Arial",27), fg="brown") # same way bg |
| 94 | +# lbl1.place(x=80, y=505) |
| 95 | +# |
| 96 | +# # name Entry Box |
| 97 | +# entry1 = Entry(font=("Arial", 30), fg='orange', bg="light yellow", borderwidth=3, width=30) |
| 98 | +# entry1.place(x=265, y=500) |
| 99 | + |
| 100 | +def open_img(): |
| 101 | + global img |
| 102 | + filename = filedialog.askopenfilename(title="Select file") |
| 103 | + |
| 104 | + img = cv2.imread(filename, 1) |
| 105 | + |
| 106 | + cv2.namedWindow("Image") |
| 107 | + cv2.setMouseCallback("Image", click) |
| 108 | + |
| 109 | + cv2.imshow("Image", img) |
| 110 | + cv2.waitKey(0) |
| 111 | + cv2.destroyAllWindows() |
| 112 | + |
| 113 | +# Select Button |
| 114 | +selectb=Button(window1, text="SELECT",command=open_img, font=("Arial", 25), bg = "light green", fg = "blue") |
| 115 | +selectb.place(x=150, y = 550) |
| 116 | + |
| 117 | +def exit_win1(): |
| 118 | + if mbox.askokcancel("Exit", "Do you want to exit?"): |
| 119 | + window1.destroy() |
| 120 | + |
| 121 | +# exit Button |
| 122 | +exitb=Button(window1, text="EXIT",command=exit_win1, font=("Arial", 25), bg = "red", fg = "blue") |
| 123 | +exitb.place(x=700, y = 550) |
| 124 | + |
| 125 | +window1.protocol("WM_DELETE_WINDOW", exit_win1) |
| 126 | +window1.mainloop() |
0 commit comments