计算机视觉-滤镜小程序

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()
相关推荐
2401_8315017313 分钟前
Python学习之day03学习(文件和异常)
开发语言·python·学习
可触的未来,发芽的智生37 分钟前
触摸未来2025.10.06:声之密语从生理构造到神经网络的声音智能革命
人工智能·python·神经网络·机器学习·架构
Zwb29979238 分钟前
Day 24 - 文件、目录与路径 - Python学习笔记
笔记·python·学习
hui函数1 小时前
python全栈(基础篇)——day03:后端内容(字符串格式化+简单数据类型转换+进制的转换+运算符+实战演示+每日一题)
开发语言·后端·python·全栈
动能小子ohhh1 小时前
AI智能体(Agent)大模型入门【6】--编写fasteAPI后端请求接口实现页面聊天
人工智能·python·深度学习·ai编程
SCBAiotAigc1 小时前
huggingface里的数据集如何下载呢?
人工智能·python
AntBlack1 小时前
PyQtInspect : 推荐一个好用的 PythonQT 界面 Debug 工具
python·pyqt
flashlight_hi1 小时前
LeetCode 分类刷题:1901. 寻找峰值 II
python·算法·leetcode
fwerfv3453452 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
蒋星熠2 小时前
反爬虫机制深度解析:从基础防御到高级对抗的完整技术实战
人工智能·pytorch·爬虫·python·深度学习·机器学习·计算机视觉