python 使用tkinter制作界面,OpenCV基础制作的交互式图片处理

import cv2
import numpy as np

#灰度图转换
def tra():
    from PIL import Image
    grayimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    cv2.imwrite("grayimg.jpg",grayimg)
    img_tra_0 = Image.open('grayimg.jpg') 
    img_tra_0.show()

#选择限定角度旋转
def rot():
    from PIL import Image
    w = t_rot.get()
    w = int(w)
    (img_h,img_w) = img.shape[:2]
    px,py = img_h/2,img_w/2
    M = cv2.getRotationMatrix2D((int(px),int(py)),w,1)
    img_rot = cv2.warpAffine(img,M,(int(px),int(py)))
    cv2.imwrite("img_rot.jpg",img_rot)
    img_rot_0 = Image.open('img_rot.jpg') 
    img_rot_0.show()

#镜像图像
def mirhor():
    from PIL import Image
    img_mirhor=cv2.flip(img,1)
    cv2.imwrite('img_mirhor.jpg',img_mirhor)
    img_mirhor_0 = Image.open('img_mirhor.jpg') 
    img_mirhor_0.show()

#去椒盐化(中值滤波)
def median():
    from PIL import Image
    median = cv2.medianBlur(img, 11)
    cv2.imwrite("median.jpg", median)
    img_median_0 = Image.open('median.jpg') 
    img_median_0.show()

#模糊化
def dim():
    from PIL import Image
    dim = cv2.blur(img,(9,9))
    cv2.imwrite("dim.jpg",dim)
    img_dim = Image.open('dim.jpg') 
    img_dim.show()

#漫画化
def threshold():
    from PIL import Image
    thresh = cv2.adaptiveThreshold(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY), 255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,9, 3)
    cv2.imwrite("thresh.jpg", thresh)
    img_thresh = Image.open('thresh.jpg') 
    img_thresh.show()

#图像变亮

def sums()
    from PIL import Image
    sums = cv2.add(img,50)
    cv2.imwrite("sums.jpg",sums)
    img_sums = Image.open('sums.jpg') 
    img_sums.show()

#图像变暗
def sub():
    from PIL import Image
    sub = cv2.subtract(img,50)
    cv2.imwrite("sub.jpg", sub)
    img_sub = Image.open('sub.jpg') 
    img_sub.show()

#读取图片
def read():
    from PIL import Image
    name = t_main.get()
    global img
    img = cv2.imread(name)
    t_main.delete(0, END)
    txt.insert(END,img)
    img0 = Image.open(name) 
    img0.show()
    tk1()

from tkinter import *
from tkinter.simpledialog import *

def tk1():
    from PIL import Image
    tk1 = Toplevel(root)
    tk1.title('图像处理器')
    tk1.geometry('300x300')
    b_tra = Button(tk1,text='转换为灰度图',command=tra)
    b_tra.place(x=100,y=0)
    b_rot0 = Button(tk1,text='选择限定角度旋转',command=tk2)
    b_rot0.place(x=100,y=30)
    b_mirhor = Button(tk1,text='镜像图像',command=mirhor)
    b_mirhor.place(x=100,y=60)
    b_median = Button(tk1,text='去椒盐化',command=median)
    b_median.place(x=100,y=90)
    b_dim = Button(tk1,text='模糊化',command=dim)
    b_dim.place(x=100,y=120)
    b_thresh = Button(tk1,text='漫画化',command=threshold)
    b_thresh.place(x=100,y=150)
    b_sums = Button(tk1,text='图片变亮',command=sums)
    b_sums.place(x=100,y=180)
    b_sub = Button(tk1,text='图片变暗',command=sub)
    b_sub.place(x=100,y=210)
    root.mainloop()

def tk2():
    from PIL import Image
    tk2 = Toplevel(root)
    tk2.title('旋转角')
    tk2.geometry('300x300')
    lb_rot = Label(tk2,text='请输入图片的旋转角度:')
    lb_rot.place(x=20,y=100)
    global t_rot
    t_rot = Entry(tk2)
    t_rot.delete(0, END)
    t_rot.place(x=130,y=100)
    b_rot = Button(tk2,text='开始旋转',command=rot)
    b_rot.place(x=120,y=130)

#主界面
from PIL import Image
root = Tk()
root.title('图像处理器')
root.geometry('300x300')
lb_main = Label(root,text='请输入图片名字:')
lb_main.place(x=0,y=0)
t_main = Entry(root)
t_main.place(x=100,y=0)
b_main = Button(root,text='开始',command=read)
b_main.place(x=120,y=50)
txt = Text(root)
txt.place(rely=0.6, relheight=0.4)
root.mainloop()

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值