计算机视觉-滤镜小程序

python 复制代码
import os

import tkinter

import cv2

from PIL import Image, ImageTk

from tkinter import filedialog, Frame

import numpy as np



class Application(Frame):

    def __init__(self, master=None):

        self.camera_height = 300

        self.camera_width = 500

        self.Source_Img_Label = None

        self.Source_Img = None

        self.py_path = os.path.abspath(os.path.dirname(__file__))

        Frame.__init__(self, master, bg='white')

        self.pack(expand=tkinter.YES, fill=tkinter.BOTH)

        self.window_init()

        self.String_var = tkinter.StringVar()

        self.createWidgets()


    def window_init(self):

        self.master.title('滤镜小程序')

        self.master.configure(bg='white')

        width, height = (self.camera_width, self.camera_height)

        self.master.geometry("{}x{}".format(width, height))


    def createWidgets(self):

        # 左侧面板:操作按钮

        self.fml = Frame(self, bg='white')

        self.fml_top = Frame(self.fml, bg='white')

        self.fml_buttons = Frame(self.fml, bg='white')


        # 文件路径输入框

        self.Img_Path_Text = tkinter.Entry(self.fml_top, textvariable=self.String_var, borderwidth=1, state=tkinter.DISABLED)

        self.Img_Path_Text.pack(side='left', fill=tkinter.X, expand=True)


        # 选择文件按钮

        self.Img_Path_Button = tkinter.Button(self.fml_top, text='选择', command=self.AskPicture)

        self.Img_Path_Button.pack(side='right', padx=5)


        # 功能按钮

        self.Outline_Button = tkinter.Button(self.fml_buttons, text='轮廓', command=self.outlinePicture)

        self.Outline_Button.pack(side='left', padx=5, expand=True)


        self.Sharpen_Button = tkinter.Button(self.fml_buttons, text='锐化', command=self.sharpenPicture)

        self.Sharpen_Button.pack(side='left', padx=5, expand=True)


        self.Emboss_Button = tkinter.Button(self.fml_buttons, text='浮雕', command=self.embossPicture)

        self.Emboss_Button.pack(side='left', padx=5, expand=True)


        self.fml_top.pack(side=tkinter.TOP, fill=tkinter.X)

        self.fml_buttons.pack(side=tkinter.TOP, fill=tkinter.X)

        self.fml.pack(side=tkinter.LEFT, fill=tkinter.Y)


        # 右侧面板:图像显示区域

        self.fm2 = Frame(self, bg='white')

        self.Source_Img_Label = tkinter.Label(self.fm2, bg='white', image=None, width=200, height=200)

        self.Source_Img_Label.image = None


        # 使用 pack 方法并设置 pady 参数以实现垂直居中

        self.Source_Img_Label.pack(side='top', pady=(self.camera_height - 200) // 2)

        self.fm2.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)


    def CvtPIL(self, srcImg):

        Rgb_Img = cv2.cvtColor(srcImg, cv2.COLOR_BGR2RGB)  # 正确的颜色空间转换

        Rgb_Img = Image.fromarray(Rgb_Img)

        Rgb_Img = ImageTk.PhotoImage(Rgb_Img)

        self.Source_Img_Label.configure(image=Rgb_Img)

        self.Source_Img_Label.image = Rgb_Img


    def AskPicture(self):

        Picture_Path = filedialog.askopenfilename()

        if not Picture_Path:

            self.String_var.set('未选择文件')

            return

        self.String_var.set(Picture_Path)

        self.Source_Img = cv2.imread(Picture_Path)

        if self.Source_Img is None:

            self.String_var.set('文件读取错误')

            return

        self.CvtPIL(self.Source_Img)


    def outlinePicture(self):

        if self.Source_Img is None:

            self.String_var.set('文件选择错误')

            return

        kernel = np.array([[-1, -1, -1],

                           [-1, 8, -1],

                           [-1, -1, -1]], dtype="float32")

        dstimg = cv2.filter2D(self.Source_Img, -1, kernel)

        self.CvtPIL(dstimg)


    def embossPicture(self):

        if self.Source_Img is None:

            self.String_var.set('文件选择错误')

            return

        kernel = np.array([[-2, -1, 0],

                           [-1, 1, 1],

                           [0, 1, 2]], dtype="float32")

        dstimg = cv2.filter2D(self.Source_Img, -1, kernel)

        self.CvtPIL(dstimg)


    def sharpenPicture(self):

        if self.Source_Img is None:

            self.String_var.set('文件选择错误')

            return

        kernel = np.array([[-1, -1, -1],

                           [-1, 9, -1],

                           [-1, -1, -1]], dtype="float32")

        dstimg = cv2.filter2D(self.Source_Img, -1, kernel)

        self.CvtPIL(dstimg)



if __name__ == '__main__':

    app = Application()

    app.mainloop()
相关推荐
2601_9679801111 分钟前
Python中flatten( )函数及函数用法详解
python·numpy·数组操作·flatten函数·列表处理
xbzb15 分钟前
自定义 yum 软件源:Linux 上 createrepo,Windows 上纯 Python 也能造
linux·运维·windows·python·yum·createrepo
合尘猫4 小时前
图片表格变结构化数据:OCR 后的三个坑
python·ocr
小小张说故事4 小时前
Python 里弱引用到底弱在哪:拆开 weakref.ref、回调陷阱和 WeakValueDictionary
python
用户019027581614 小时前
用 DuckDB 存查 A 股 K 线:零服务、SQL 直接分析
python
程序员果子5 小时前
本体构建方法
人工智能·python·知识图谱·agent框架·本体
天天被压力6 小时前
【A股分钟级高频数据完全手册 #11】券商实时行情数据接口:实时交易(券商源)接入与字段
java·人工智能·python
总裁余(余登武)6 小时前
【python项目】:1操作EXCEl自动化开发
python·自动化·excel
芝梵家电数据笔记6 小时前
用字段表核对暖菜板长期使用条件:结构、暖区、质保边界
python·家电·数据校验·dataclass·选购
天天被压力6 小时前
【A股分钟级高频数据完全手册 #12】股票五档盘口数据接口:买卖五档实时盘口解析
java·人工智能·python