{"id":370,"date":"2024-12-08T13:20:14","date_gmt":"2024-12-08T05:20:14","guid":{"rendered":"\/?p=370"},"modified":"2024-12-08T13:20:48","modified_gmt":"2024-12-08T05:20:48","slug":"python-opencvtkinter","status":"publish","type":"post","link":"\/?p=370","title":{"rendered":"Python-Opencv+tkinter"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u8272\u57df\u8c03\u6574<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">import tkinter as tk<br>import cv2 as cv<br>from tkinter import messagebox<br>import  numpy<br>inpath = 'InImage\/1.png'<br>outpath = 'OutImage\/R.png'<br><br>i1 = []<br><br><br>def cutphoto(*args):<br>    global i1<br>    try:<br>        iamge = cv.imread(inpath, 0)  # \u8bfb\u53d6\u56fe\u7247<br>        image = cv.resize(iamge,args) #\u8bbe\u7f6e\u56fe\u7247\u5927\u5c0f<br>        ret,i1 = cv.threshold(image,int(scale.get()),255,cv.THRESH_TRUNC) #\u8c03\u6574\u8272\u57df<br>        cv.namedWindow('Image', cv.WINDOW_AUTOSIZE)  # \u521b\u5efa\u7a97\u53e3\u540d<br>        cv.imshow(\"Image\",i1)  # \u663e\u793a\u56fe\u7247<br>    except Exception as e:<br>        print(e)<br><br><br>def RGB(*args):<br>    if ipt1.get() and ipt2.get():<br>        w = int(ipt1.get())<br>        h = int(ipt2.get())<br>        if w &gt; 2000 or h &gt; 2000:<br>            messagebox.showinfo(title='\u63d0\u793a',message='\u8bbe\u7f6e\u56fe\u7247\u8fc7\u5927')<br>        else:<br>            cutphoto(w,h)<br>    else:<br>        w = 500<br>        h = 500<br>        cutphoto(w, h)<br><br>def savepr():<br>    RGB()<br>    cv.imwrite(outpath, i1)<br>    tk.messagebox.showinfo(title='\u63d0\u793a',message='\u4fdd\u5b58\u6210\u529f')<br><br>parent = tk.Tk()<br>parent.title(\"RGB\u8272\u57df\u5206\u89e3\")<br># \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u3001\u5c45\u4e2d<br>width = 461<br>height = 387<br>screenwidth = parent.winfo_screenwidth()<br>screenheight = parent.winfo_screenheight()<br>geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) \/ 2, (screenheight - height) \/ 2)<br>parent.geometry(geometry)<br><br>parent.resizable(width=False, height=False)<br><br>label1 = tk.Label(parent, text=\"\u56fe\u7247\u5927\u5c0f\u8bbe\u7f6e\", anchor=\"center\", )<br>label1.place(x=161, y=28, width=129, height=30)<br><br>label2 = tk.Label(parent, text=\"\u957f\u5ea6\uff1a\", anchor=\"center\", )<br>label2.place(x=96, y=101, width=50, height=30)<br><br><br>label3 = tk.Label(parent, text=\"\u5bbd\u5ea6\uff1a\", anchor=\"center\", )<br>label3.place(x=95, y=162, width=50, height=30)<br><br>label4 = tk.Label(parent, text=\"px\", anchor=\"center\", )<br>label4.place(x=313, y=98, width=50, height=30)<br><br>label5 = tk.Label(parent, text=\"px\", anchor=\"center\", )<br>label5.place(x=313, y=165, width=50, height=30)<br><br>ipt1 = tk.Entry(parent, )<br>ipt1.place(x=165, y=97, width=150, height=30)<br><br><br>ipt2 = tk.Entry(parent, )<br>ipt2.place(x=165, y=163, width=150, height=30)<br><br>scale = tk.Scale(parent, to=255, orient=tk.HORIZONTAL, command=RGB)<br>scale.set(127)<br>scale.place(x=110, y=226, width=241, height=30)<br><br>btn = tk.Button(parent, text=\"\u786e\u8ba4\", takefocus=False, command=RGB)<br>btn.place(x=102, y=315, width=100, height=30)<br><br>btn1 = tk.Button(parent, text=\"\u4fdd\u5b58\", takefocus=False, command=savepr)<br>btn1.place(x=287, y=315, width=100, height=30)<br><br>tk.mainloop()<br><br><br><br><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8272\u57df\u8c03\u6574 import tkinter as tkimport cv2 as cvfrom tkinter i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":371,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-370","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/370"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=370"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":372,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions\/372"}],"wp:featuredmedia":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/media\/371"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}