简体   繁体   中英

Tkinter + opencv choose image

I have a script where I choose an image " image = cv2.imread("1.jpg")" and then I have a menu (1, 2, 3) where I can choose several transformations of these images.

How can I get a tkinter application where I can choose an image and then be able to make these options.

I can only choose images from a UI, but then I can't follow the script

From this script you will be able to select an image and apply opencv filters. If you want to make the options create 3 another buttons and implement cv inside

  # import the necessary packages
    from tkinter import *
    from PIL import Image
    from PIL import ImageTk
    from tkinter import filedialog as tkFileDialog
    import cv2
    def select_image():
        # grab a reference to the image panels
        global panelA, panelB
        # open a file chooser dialog and allow the user to select an input
        # image
        path = tkFileDialog.askopenfilename()
        # ensure a file path was selected
        if len(path) > 0:
            # load the image from disk, convert it to grayscale, and detect
            # edges in it
            image = cv2.imread(path)
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            edged = cv2.Canny(gray, 50, 100)
            # OpenCV represents images in BGR order; however PIL represents
            # images in RGB order, so we need to swap the channels
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            # convert the images to PIL format...
            image = Image.fromarray(image)
            edged = Image.fromarray(edged)
            # ...and then to ImageTk format
            image = ImageTk.PhotoImage(image)
            edged = ImageTk.PhotoImage(edged)
                # if the panels are None, initialize them
            if panelA is None or panelB is None:
                # the first panel will store our original image
                panelA = Label(image=image)
                panelA.image = image
                panelA.pack(side="left", padx=10, pady=10)
                # while the second panel will store the edge map
                panelB = Label(image=edged)
                panelB.image = edged
                panelB.pack(side="right", padx=10, pady=10)
            # otherwise, update the image panels
            else:
                # update the pannels
                panelA.configure(image=image)
                panelB.configure(image=edged)
                panelA.image = image
                panelB.image = edged
            # initialize the window toolkit along with the two image panels
    root = Tk()
    panelA = None
    panelB = None
    # create a button, then when pressed, will trigger a file chooser
    # dialog and allow the user to select an input image; then add the
    # button the GUI
    btn = Button(root, text="Select an image", command=select_image)
    btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")
    # kick off the GUI
    root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM