图像的简单操作(gui)

发布时间:2023年12月20日

需要导入的包

import cv2
import tkinter as tk
import numpy as np
from PIL import Image, ImageTk
from gui import newEntry

定义每个功能函数

# 读取图片
def get_image():
    img = cv2.imread(e1.get())
    video_loop(dst=img)
    return img


# 沿X轴翻转
def x_fig():
    dst1 = cv2.flip(get_image(), 0)
    video_loop(dst=dst1)
    return dst1


# 沿Y轴翻转
def y_fig():
    dst1 = cv2.flip(get_image(), 1)
    video_loop(dst=dst1)
    return dst1


# 同时沿X轴、Y轴翻转
def xy_fig():
    dst1 = cv2.flip(get_image(), -1)
    video_loop(dst=dst1)
    return dst1


def f_exit():  # 退出按钮
    exit()


# 平移照片
def move_img():
    img = get_image()
    height, width = img.shape[:2]  # 读取原图像的长和宽
    str1 = e3.get()
    list1 = str1.split(',')
    x = list1[0]  # 自定义转换矩阵M的x轴移动值
    y = list1[1]  # 自定义转换矩阵M的y轴移动值
    M = np.float32([[1, 0, x], [0, 1, y]])  # 构造转换矩阵M
    move = cv2.warpAffine(img, M, (width, height))  # 平移映射
    video_loop(move)


# 旋转图片
def xz():
    img = get_image()
    height, width = img.shape[:2]  # 读取原图像的长和宽
    M = cv2.getRotationMatrix2D((width / 2, height / 2), int(e2.get()), 0.6)  # 以中心为原点,逆时针旋转45°,且缩小为原图的0.6倍,获得转移矩阵M
    rotate = cv2.warpAffine(img, M, (width, height))  # 旋转映射
    video_loop(rotate)

再图像化界面中显示照片的变化

def video_loop(dst):
    cv2.waitKey(1)
    cv2image = cv2.cvtColor(dst, cv2.COLOR_BGR2RGBA)  # 转换颜色从BGR到RGBA
    current_image = Image.fromarray(cv2image)  # 将图像转换成Image对象
    imgtk = ImageTk.PhotoImage(image=current_image)
    panel.imgtk = imgtk
    panel.config(image=imgtk)
    return dst

创建一个窗口对象

window = tk.Tk()
window.title('图像的操作')  # 窗口标题
window.geometry('1000x500')  # 这里的乘是小x

输入图片文件的位置

# 在图形界面上设定标签,类似于一个提示窗口的作用
var = tk.StringVar()
l = tk.Label(window, text='输入图像位置', bg='pink', fg='white', font=('Arial', 12), width=50, height=2)
# 说明: bg为背景,fg为字体颜色,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高
# 放置l控件
l.pack()

v1 = tk.StringVar()
e1 = tk.Entry(window, textvariable=v1, width=50)
e1.place(x=300, y=120 - 69)

为了提高用不的体验,对用户需要输入的文本框进行提示需要拓展一个小的功能
这个一段代码需要创建一个新的文件然后再次引用

from tkinter import *

class newEntry(Entry):
    def __init__(self, master=None, placeholder="PLACEHOLDER", color="grey"):
        super().__init__(master)

        self.placeholder = placeholder
        self.placeholder_color = color
        self.default_fg_color = self["fg"]

        self.bind("<FocusIn>", self.foc_in)
        self.bind("<FocusOut>", self.foc_out)

        self.put_placeholder()

    def put_placeholder(self):
        self.insert(0, self.placeholder)
        self["fg"] = self.placeholder_color

    def foc_in(self, *args):
        if self["fg"] == self.placeholder_color:
            self.delete("0", "end")
            self["fg"] = self.default_fg_color

    def foc_out(self, *args):
        if not self.get():
            self.put_placeholder()

设置键入平移的距离和旋转按钮

v2 = tk.StringVar()
e2 = newEntry(window, '输入旋转的度数')
e2.place(x=650, y=155)
# e2.insert(0, '输入旋转的度数')

# 在窗口界面设置放置Button按键并绑定处理函数
button_a = tk.Button(window, text='读取图片', font=('Arial', 12), width=10, height=1, command=get_image)
button_a.place(x=610, y=120 - 75)

设置键入旋转的度数和旋转按钮

v3 = tk.StringVar()
e3 = newEntry(window, "输入两个数值用英文逗号分开")
e3.place(x=650, y=215)
# e3.insert(0, "输入两个数值用英文逗号分开")

button_b = tk.Button(window, text='旋转', font=('Arial', 12), width=10, height=2, command=xz)
button_b.place(x=800, y=520 - 380)

设置沿着不同轴进行翻转

button_b = tk.Button(window, text='同时沿X轴、Y轴翻转', font=('Arial', 12), width=20, height=2, command=xy_fig)
button_b.place(x=750, y=520 - 260)

button_b = tk.Button(window, text='沿Y轴翻转', font=('Arial', 12), width=10, height=2, command=y_fig)
button_b.place(x=800, y=520 - 200)

button_b = tk.Button(window, text='沿X轴翻转', font=('Arial', 12), width=10, height=2, command=x_fig)
button_b.place(x=800, y=520 - 140)

退出按钮

button_b = tk.Button(window, text='退出', font=('Arial', 12), width=10, height=2, command=f_exit)
button_b.place(x=800, y=520 - 80)

图像模块的显示设置

panel = tk.Label(window, width=360, height=350)  # 图像模块大小
panel.place(x=200, y=100)  # 图像模块的位置
window.config(cursor="arrow")

#  窗口循环,用于显示
window.mainloop()

优化前

# -*- coding: UTF-8 -*-
# @Project :线性回归的多元回归 
# @File    :10.py
# @Author  :于金龙
# @IDE     :PyCharm 
# @Date    :2023/12/20 10:15
import cv2
import tkinter as tk
import numpy as np
from PIL import Image, ImageTk

# 读取图片
def get_image():
    img = cv2.imread(e1.get())
    video_loop(dst=img)
    return img


# 沿X轴翻转
def x_fig():
    dst1 = cv2.flip(get_image(), 0)
    video_loop(dst=dst1)
    return dst1


# 沿Y轴翻转
def y_fig():
    dst1 = cv2.flip(get_image(), 1)
    video_loop(dst=dst1)
    return dst1


# 同时沿X轴、Y轴翻转
def xy_fig():
    dst1 = cv2.flip(get_image(), -1)
    video_loop(dst=dst1)
    return dst1


def f_exit():  # 退出按钮
    exit()


# 平移照片
def move_img():
    img = get_image()
    height, width = img.shape[:2]  # 读取原图像的长和宽
    str1 = e3.get()
    list1 = str1.split(',')
    x = list1[0]  # 自定义转换矩阵M的x轴移动值
    y = list1[1]  # 自定义转换矩阵M的y轴移动值
    M = np.float32([[1, 0, x], [0, 1, y]])  # 构造转换矩阵M
    move = cv2.warpAffine(img, M, (width, height))  # 平移映射
    video_loop(move)


# 旋转图片
def xz():
    img = get_image()
    height, width = img.shape[:2]  # 读取原图像的长和宽
    M = cv2.getRotationMatrix2D((width / 2, height / 2), int(e2.get()), 0.6)  # 以中心为原点,逆时针旋转45°,且缩小为原图的0.6倍,获得转移矩阵M
    rotate = cv2.warpAffine(img, M, (width, height))  # 旋转映射
    video_loop(rotate)


def video_loop(dst):  # 用于在label内动态展示摄像头内容(摄像头嵌入控件)
    cv2.waitKey(1)
    cv2image = cv2.cvtColor(dst, cv2.COLOR_BGR2RGBA)  # 转换颜色从BGR到RGBA
    current_image = Image.fromarray(cv2image)  # 将图像转换成Image对象
    imgtk = ImageTk.PhotoImage(image=current_image)
    panel.imgtk = imgtk
    panel.config(image=imgtk)
    return dst


window = tk.Tk()
window.title('图像的操作')  # 窗口标题
window.geometry('1000x500')  # 这里的乘是小x

# 在图形界面上设定标签,类似于一个提示窗口的作用
var = tk.StringVar()
l = tk.Label(window, text='输入图像位置', bg='pink', fg='white', font=('Arial', 12), width=50, height=2)
# 说明: bg为背景,fg为字体颜色,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高
# 放置l控件
l.pack()

v1 = tk.StringVar()
e1 = tk.Entry(window, textvariable=v1, width=50)
e1.place(x=300, y=120 - 69)

v2 = tk.StringVar()
e2 = tk.Entry(window, textvariable=v2, width=20)
e2.place(x=650, y=155)
# e2.insert(0, '输入旋转的度数')

# 在窗口界面设置放置Button按键并绑定处理函数
button_a = tk.Button(window, text='读取图片', font=('Arial', 12), width=10, height=1, command=get_image)
button_a.place(x=610, y=120 - 75)

button_b = tk.Button(window, text='旋转', font=('Arial', 12), width=10, height=2, command=xz)
button_b.place(x=800, y=520 - 380)

v3 = tk.StringVar()
e3 = tk.Entry(window, textvariable=v3, width=20)
e3.place(x=650, y=215)
# e3.insert(0, "输入两个数值用英文逗号分开")

button_b = tk.Button(window, text='平移映射', font=('Arial', 12), width=10, height=2, command=move_img)
button_b.place(x=800, y=520 - 320)

button_b = tk.Button(window, text='同时沿X轴、Y轴翻转', font=('Arial', 12), width=20, height=2, command=xy_fig)
button_b.place(x=750, y=520 - 260)

button_b = tk.Button(window, text='沿Y轴翻转', font=('Arial', 12), width=10, height=2, command=y_fig)
button_b.place(x=800, y=520 - 200)

button_b = tk.Button(window, text='沿X轴翻转', font=('Arial', 12), width=10, height=2, command=x_fig)
button_b.place(x=800, y=520 - 140)

button_b = tk.Button(window, text='退出', font=('Arial', 12), width=10, height=2, command=f_exit)
button_b.place(x=800, y=520 - 80)

panel = tk.Label(window, width=360, height=350)  # 图像模块大小
panel.place(x=200, y=100)  # 图像模块的位置
window.config(cursor="arrow")

#  窗口循环,用于显示
window.mainloop()

优化前效果
在这里插入图片描述

愿君前程似锦,未来可期去💯,感谢您的阅读,如果对您有用希望您留下宝贵的点赞和收藏
本文章为本人学习笔记,如有请侵权联系,本人会立即删除侵权文章。可以一起学习共同进步谢谢,如有请侵权联系,本人会立即删除侵权文章。

红框部分就是我们优化实现的部分
在这里插入图片描述

文章来源:https://blog.csdn.net/yujinlong2002/article/details/135108681
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。