0

This is a desktop application in which by clicking the choose file button, i should be able to select the image from desktop,open it and crop it by drawing a rectangle by the mouse and crop it by pressing the "c" key from the keyboard.The cropped picture will appear on a new window. I want to develop a code for this,but after pressing the choose file button, it is asking for the parameters to be passed in the click and crop function in this line"choose_btn=Button(app,text="Choose file",width=12,command=click_and_crop)".

import cv2
import PIL
from PIL import Image
from tkinter import *
from tkinter import filedialog

app=Tk()

refPt = []
cropping = False
a=0

def click_and_crop(event, x, y, flags, param):


    global refPt, cropping, a



    if event == cv2.EVENT_LBUTTONDOWN:
        refPt = [(x, y)]
        cropping = True



    elif event == cv2.EVENT_LBUTTONUP:

        refPt.append((x, y))
        cropping = False

        cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
        cv2.imshow("image", image)
        a=1


hello_text=StringVar()
hello_labeltext=Label(app,text="Hi there!!",font=("bold",30),pady=20,padx=50)
hello_labeltext.grid(row=0,column=0)

welcome_text=StringVar()
welcome_labeltext=Label(app,text=("Welcome to the Gallery"),font=("bold",20),padx=70)
welcome_labeltext.grid(row=1,column=0)

upload_text=StringVar()
upload_labeltext=Label(app,text=("Click on the Upload Button to upload images"),font=("bold",20),padx=70)
upload_labeltext.grid(row=2,column=0)

choose_btn=Button(app,text="Choose file",width=12,command=click_and_crop)
choose_btn.grid(row=3,column=0,pady=20)

app.title("Image Enhancer")
app.geometry("700x350")

if(a!=0):
        file_path=filedialog.askopenfilename()
        image = cv2.imread(file_path)
        clone = image.copy()
        cv2.namedWindow("image")
        cv2.setMouseCallback("image", click_and_crop)

while True:

    cv2.imshow("image", image)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("r"):
        image = clone.copy()
    elif key == ord("c"):
        break

if len(refPt) == 2:
    roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
    cv2.imshow("ROI", roi)
    cv2.waitKey(0)

cv2.destroyAllWindows()
app.mainloop()
  • You need to pass parameters to `click_and_crop`. Take a look at [tkinter-callbacks](http://effbot.org/zone/tkinter-callbacks.htm) for information on how to use `lambda` to achieve this. – kusz Mar 26 '20 at 15:07
  • I can't imagin that you can mix `tkinter` with `cv2`. Both have there own `mainloop()` which prevents the other to run. Read [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) and [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Mar 26 '20 at 15:44
  • Hey mkusz,As I'm a beginner,I do not know how to use lambda for little complex functions like click_and_crop,could you please help me a a bit more with this? – Aishanya Kushwaha Mar 27 '20 at 13:39
  • To correctly pass parameters to `click_and_crop` you need to use lambda like this: `choose_btn=Button(app,text="Choose file",width=12,command=lambda: click_and_crop(event, x, y, flags, param))`. Also make sure to @ someone when replying to them so they will receive a notification. – kusz Apr 09 '20 at 15:40

0 Answers0