Skip to content

Commit a3d761d

Browse files
committed
opencv-with-tkinter
1 parent 71212d2 commit a3d761d

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

cv-Tkinter-GUI/opencv-with-tkinter.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2018/2/8 15:56
3+
# @Author : play4fun
4+
# @File : opencv-with-tkinter.py
5+
# @Software: PyCharm
6+
7+
"""
8+
opencv-with-tkinter.py:
9+
https://www.pyimagesearch.com/2016/05/23/opencv-with-tkinter/
10+
11+
不需要
12+
pip install image
13+
14+
"""
15+
16+
# import the necessary packages
17+
from tkinter import *
18+
from PIL import Image
19+
from PIL import ImageTk
20+
import tkinter.filedialog as tkFileDialog
21+
import cv2
22+
23+
24+
def select_image():
25+
# grab a reference to the image panels
26+
global panelA, panelB
27+
28+
# open a file chooser dialog and allow the user to select an input
29+
# image
30+
path = tkFileDialog.askopenfilename()
31+
32+
# ensure a file path was selected
33+
if len(path) > 0:
34+
# load the image from disk, convert it to grayscale, and detect
35+
# edges in it
36+
image = cv2.imread(path)
37+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
38+
edged = cv2.Canny(gray, 50, 100)
39+
40+
# represents images in BGR order; however PIL represents
41+
# images in RGB order, so we need to swap the channels
42+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
43+
44+
# convert the images to PIL format...
45+
image = Image.fromarray(image)
46+
edged = Image.fromarray(edged)
47+
48+
# ...and then to ImageTk format
49+
image = ImageTk.PhotoImage(image)
50+
edged = ImageTk.PhotoImage(edged)
51+
52+
# if the panels are None, initialize them
53+
if panelA is None or panelB is None:
54+
# the first panel will store our original image
55+
panelA = Label(image=image)
56+
panelA.image = image
57+
panelA.pack(side="left", padx=10, pady=10)
58+
59+
# while the second panel will store the edge map
60+
panelB = Label(image=edged)
61+
panelB.image = edged
62+
panelB.pack(side="right", padx=10, pady=10)
63+
64+
# otherwise, update the image panels
65+
else:
66+
# update the pannels
67+
panelA.configure(image=image)
68+
panelB.configure(image=edged)
69+
panelA.image = image
70+
panelB.image = edged
71+
72+
73+
# initialize the window toolkit along with the two image panels
74+
root = Tk()
75+
panelA = None
76+
panelB = None
77+
78+
# create a button, then when pressed, will trigger a file chooser
79+
# dialog and allow the user to select an input image; then add the
80+
# button the GUI
81+
btn = Button(root, text="Select an image", command=select_image)
82+
btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")
83+
84+
# kick off the GUI
85+
root.mainloop()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy