图像的简单操作(gui)

需要导入的包

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()

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

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

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

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿龙的代码在报错

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值