欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用Python给图像算法做个简单应用界面

程序员文章站 2022-08-26 20:00:07
以前在windows上做界面用mfc,现在做算法都是基于python,所以转用python的tkinter库来做。主要是能使用opencv和torch处理数据,然后在界面上显示。效果如下:主要包括3个...

以前在windows上做界面用mfc,现在做算法都是基于python,所以转用python的tkinter库来做。主要是能使用opencv和torch处理数据,然后在界面上显示。

效果如下:

主要包括3个板块,其余还有一些小功能:

1、显示固定的图片。或从电脑加载一张图片并显示(涉及到按钮的响应函数编写和弹窗)

2、下拉框和文本框的使用

3、进度条的使用(涉及到多线程)

用Python给图像算法做个简单应用界面

用Python给图像算法做个简单应用界面

tkinter支持控件自动调整布局,但是时间比较赶就不研究了,使用固定位置布局,界面也不给调整。

控件名称

  • buttom 按钮,软件交互功能实现
  • label (叫什么不重要),用来显示图片或文字
  • combobox 下拉框,做选择
  • entry 文本框,做文本输入
  • progressbar 进度条,算法跑起来之后显示进度
  • labelframe (...),灰色的框框,模块化布局控件

代码如下:

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.messagebox
import tkinter.filedialog
import cv2 as cv
from pil import image, imagetk
import time
import threading
 
relief=['flat', 'raised', 'sunken', 'solid', 'ridge', 'groove']
cursor=['arrow','circle','clock','cross','dotbox','exchange',
        'fleur','heart','man','mouse','pirate','plus',
        'shuttle','sizing','spider','spraycan','star','target',
        'tcross','trek','watch']
 
def pil2cv(im):
    im = im[:, :, ::-1]
    return imagetk.photoimage(image.fromarray(im))
 
def buttom1_callback():
    filename = tk.filedialog.askopenfilename() #弹出文件选择对话框
    if filename=='': #用户没有选择任何文件
        return
    new_img = cv.imread(filename)
    if new_img is none:
        tk.messagebox.showerror('抱歉', '图片加载失败!')
        return
    new_img = cv.resize(new_img, (130, 120))
    new_img = pil2cv(new_img)
    #后面两句实现图片切换显示
    label2.configure(image=new_img, width=130, height=120)
    label2.image = new_img
    tk.messagebox.showinfo('提示','加载图片完成!')
 
def buttom2_callback():
    info = combobox1.get()
    param = entry1.get()
    tk.messagebox.showwarning('警告', '你选择了:'+info+' '+param)
 
def process_code(delay):
    for i in range(100):
        progressbar1['value'] = i+1
        root.update()
        time.sleep(delay)
    buttom3.configure(text='开始处理', state='normal')
    tk.messagebox.showinfo('提示', '处理完成!')
    progressbar1.configure(value=0)
 
def buttom3_callback():
    yn = tk.messagebox.askyesno('警告','是否需要开始处理?')
    if not yn:
        return
 
    buttom3.configure(text='处理中...', state='disabled') #控件失效
    delay = 0.01
 
    # 单独开一个线程,绑定线程函数process_code,参数后面的','很关键
    # 不开线程界面会进入处理函数死循环,用户体验不太好
    t = threading.thread(target=process_code, args=(delay,))
    t.start()
 
def buttom4_callback():
    global page_count
    if page_count<=0:
        page_count = 0
        return
    else:
        page_count -= 1
        label4.configure(text='第'+str(page_count)+'页')
    return
 
def buttom5_callback():
    global page_count
    if page_count>=100:
        page_count = 100
        return
    else:
        page_count += 1
        label4.configure(text='第' + str(page_count) + '页')
    return
 
#上面是控件的响应函数
################################################################################
#下面是界面控件的布局
 
#主界面
root = tk.tk()
root.title('python界面测试') #修改界面标题
root.iconbitmap('img/tm.ico') #修改界面ico
root.geometry('800x500') #设定界面尺寸 hxw
root.resizable(width=false, height=false) #不允许调整窗口大小,不固定删除此行
 
#添加两个板块边界框
frame1 = tk.labelframe(root, height=200, width=145)
frame1.place(x=15, y=100)
frame2 = tk.labelframe(root, text="结果显示", height=400, width=620)
frame2.place(x=170, y=5)
 
#添加图片显示框、加载图片框、加载图片按钮
img = cv.imread('img/title.jpg') #opencv加载图片
img = cv.resize(img, (140,70)) #图片缩放
img = pil2cv(img) #opencv格式转pillow
label1 = tk.label(root, image=img) #初始化默认图片
label1.place(x=15, y=20) #图片显示框在界面上的位置
 
label2 = tk.label(root,
                  width=18,height=7, #控件大小(注意单位不是像素)
                  bg="white") #默认白色背景
label2.place(x=20,y=110) #图片显示框在界面上的位置
 
buttom1 = tk.button(root,
                    width=15,height=1, #按钮大小
                    text='加载检索图片', #按钮文本
                    relief=relief[3], #按钮的风格
                    command=buttom1_callback) #绑定响应函数
buttom1.place(x=25, y=250) #按钮在界面上的位置
 
#添加参数文本框、下拉框、下拉框内容输出按钮
combobox1 = ttk.combobox(root, width=17, height=1)
combobox1['value'] = ('窗前明月光','疑是地上霜','举头望明月','明月照我影')
combobox1.current(0)
combobox1.place(x=15, y=320)
 
label3 = tk.label(root, text='参数')
label3.place(x=15, y=350)
 
entry1 = ttk.entry(root, width=9) #文本框为啥没有h
entry1.place(x=50, y=350)
entry1.insert(0,'0.5')
 
buttom2 = tk.button(root,
                    width=15,height=1,
                    text='你选择了什么?',
                    relief=relief[3],
                    command=buttom2_callback)
buttom2.place(x=25, y=380)
 
#添加进度条、开始处理按钮
progressbar1 = ttk.progressbar(root, length=600, value=0, cursor=cursor[1])
progressbar1.place(x=15, y=460)
 
buttom3 = tk.button(root,
                    width=15,height=1,
                    text='开始处理',
                    relief=relief[3],
                    command=buttom3_callback)
buttom3.place(x=630, y=455)
 
#添加两个滚动按钮
buttom4 = tk.button(root,
                    width=3,height=1,
                    text='<',
                    relief=relief[1],
                    command=buttom4_callback)
buttom4.place(x=380, y=410)
 
global page_count #全局变量,用来控制页码
page_count=0
label4 = tk.label(root, text='第0页')
label4.place(x=420, y=410)
 
buttom5 = tk.button(root,
                    width=3,height=1,
                    text='>',
                    relief=relief[1],
                    command=buttom5_callback)
buttom5.place(x=470, y=410)
 
root.mainloop()
#这句话后面不能有代码

以上就是用python给图像算法做个简单应用界面的详细内容,更多关于python 应用界面的资料请关注其它相关文章!