一、在Pytorch框架的图像预处理函数

1.读取图片

im = Image.open('./test.jpg')

2.随机比例缩放

new_im = transforms.Resize((100, 200))(im)
print(f'{im.size}---->{new_im.size}')

3.随机位置剪裁

new_im = transforms.RandomCrop(100)(im)   # 裁剪出100x100的区域
new_im = transforms.CenterCrop(100)(im)

4.随机水平/垂直翻转

new_im = transforms.RandomHorizontalFlip(p=1)(im)   # p表示概率
new_im = transforms.RandomVerticalFlip(p=1)(im)

5.随机角度旋转

new_im = transforms.RandomRotation(45)(im)

6.色度、亮度、饱和度、对比度的变化

new_im = transforms.ColorJitter(brightness=1)(im)
new_im = transforms.ColorJitter(contrast=1)(im)
new_im = transforms.ColorJitter(saturation=0.5)(im)
new_im = transforms.ColorJitter(hue=0.5)(im)

7.随机的灰度化

new_im = transforms.RandomGrayscale(p=0.5)(im) #以0.5的概率进行灰度化

8.Padding(将原始图padding为正方形)

new_im = transforms.Pad((0, (im.size[0]-im.size[1])//2))(im)  # 原图为(500,313)

二、用tkinter库设计一个交互界面

1.创建一个界面窗口

win = tkinter.Tk()
win.title("picture process")
win.geometry("1280x1080")

2.图像处理函数参考上面的实现

3.设置原图像和处理后的图像展示

render = ImageTk.PhotoImage(load)
img  = tkinter.Label(win,image=render)
img.image = render
img.place(x=100,y=100)

4.设置按钮(举一个为例子)

button2 = tkinter.Button(win,text="save",command=save)
button2.place(x=600,y=100)

三、代码实现

import tkinter
import tkinter.filedialog
from PIL import Image,ImageTk
from torchvision import transforms as transforms
import os#设置图片保存路径
outfile = './out_pic'#创建一个界面窗口
win = tkinter.Tk()
win.title("picture process")
win.geometry("1280x1080")#设置全局变量
original = Image.new('RGB', (300, 400))
save_img = Image.new('RGB', (300, 400))
count =0
img2 = tkinter.Label(win)#实现在本地电脑选择图片
def choose_file():select_file = tkinter.filedialog.askopenfilename(title='选择图片')e.set(select_file)load = Image.open(select_file)load = transforms.Resize((300,400))(load)#声明全局变量global originaloriginal = loadrender = ImageTk.PhotoImage(load)img  = tkinter.Label(win,image=render)img.image = renderimg.place(x=100,y=100)#随机比例缩放
def lessen():temp = originalnew_im = transforms.Resize((100,200))(temp)print(f'{temp.size}---->{new_im.size}')render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(win,image=render)img2.image = renderimg2.place(x=800,y=100)global save_imgsave_img = new_im#随机位置裁剪
def random_cut():temp = originalnew_im = transforms.RandomCrop(100)(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(image=render)img2.image = renderimg2.place(x=800,y=100)global save_imgsave_img = new_im#中心剪裁
def center_cut():temp = originalnew_im = transforms.CenterCrop(100)(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(image=render)img2.image = renderimg2.place(x=800,y=100)global save_img1save_img = new_im#随机水平翻转
def Horizon():temp = originalnew_im = transforms.RandomHorizontalFlip(p=1)(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(image=render)img2.image = renderimg2.place(x=800,y=100)global save_img1save_img = new_im#随机垂直翻转
def Vertical():temp = originalnew_im = transforms.RandomVerticalFlip(p=1)(temp)  render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(image=render)img2.image = renderimg2.place(x=800,y=100)global save_img1save_img = new_im#随机角度旋转
def Rotation():temp = originalnew_im = transforms.RandomRotation(45)(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(image=render)img2.image = renderimg2.place(x=800,y=100)global save_img1save_img = new_im#padding为正方形
def padding():temp = originalnew_im = transforms.Pad((0, (temp.size[0]-temp.size[1])//2))(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(win,image=render)img2.image = renderimg2.place(x=800,y=100)global save_imgsave_img = new_im#随机灰度化
def random_gray():temp = originalnew_im = transforms.RandomGrayscale(p=0.5)(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(win,image=render)img2.image = renderimg2.place(x=800,y=100)global save_imgsave_img = new_im#设置亮度
def set_bright():def show_bright(ev=None):temp = originalnew_im = transforms.ColorJitter(brightness=scale.get())(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(win,image=render)img2.image = renderimg2.place(x=800,y=100)global save_imgsave_img = new_imtop = tkinter.Tk()top.geometry('250x150')top.title('亮度设置')scale = tkinter.Scale(top, from_=0, to=100, orient=tkinter.HORIZONTAL,command=show_bright)scale.set(1)scale.pack()#设置对比度
def set_contrast():def show_contrast(ev=None):temp = originalnew_im = transforms.ColorJitter(contrast=scale.get())(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(win,image=render)img2.image = renderimg2.place(x=800,y=100)global save_imgsave_img = new_imtop = tkinter.Tk()top.geometry('250x150')top.title('对比度设置')scale = tkinter.Scale(top, from_=0, to=100, orient=tkinter.HORIZONTAL,command=show_contrast)scale.set(1)scale.pack()#设置色度
def set_hue():def show_hue(ev=None):temp = originalnew_im = transforms.ColorJitter(hue=scale.get())(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(win,image=render)img2.image = renderimg2.place(x=800,y=100)global save_imgsave_img = new_imtop = tkinter.Tk()top.geometry('250x150')top.title('色度设置')scale = tkinter.Scale(top, from_=-0.5, to=0.5, resolution=0.1,orient=tkinter.HORIZONTAL,command=show_hue)scale.set(1)scale.pack()#设置饱和度
def set_saturation():def show_saturation(ev=None):temp = originalnew_im = transforms.ColorJitter(saturation=scale.get())(temp)render = ImageTk.PhotoImage(new_im)global img2img2.destroy()img2  = tkinter.Label(win,image=render)img2.image = renderimg2.place(x=800,y=100)global save_imgsave_img = new_imtop = tkinter.Tk()top.geometry('250x150')top.title('饱和度设置')scale = tkinter.Scale(top, from_=0, to=100, resolution=1,orient=tkinter.HORIZONTAL,command=show_saturation)scale.set(1)scale.pack()#保存函数
def save():global countcount += 1save_img.save(os.path.join(outfile,'test'+str(count)+'.jpg'))#显示路径
e = tkinter.StringVar()
e_entry = tkinter.Entry(win, width=68, textvariable=e)
e_entry.pack()#设置选择图片的按钮
button1 = tkinter.Button(win, text ="Select", command = choose_file)
button1.pack()#设置标签分别为原图像和修改后的图像
label1 = tkinter.Label(win,text="Original Picture")
label1.place(x=200,y=50)label2 = tkinter.Label(win,text="Modified Picture")
label2.place(x=900,y=50)#设置保存图片的按钮
button2 = tkinter.Button(win,text="save",command=save)
button2.place(x=600,y=100)#设置随机比例缩放的按钮
button3 = tkinter.Button(win,text="Random Lessen",command=lessen)
button3.place(x=600,y=150)#设置随机比例裁剪的按钮
button4 = tkinter.Button(win,text="Random Cut",command=random_cut)
button4.place(x=600,y=200)#设置center_cut按钮
button5 = tkinter.Button(win,text="Center Cut",command=center_cut)
button5.place(x=600,y=250)#设置随机水平翻转按钮
button6 = tkinter.Button(win,text="Random Horizontal Flip",command=Horizon)
button6.place(x=600,y=300)#设置随机垂直翻转按钮
button7 = tkinter.Button(win,text="Random Vertical Flip",command=Vertical)
button7.place(x=600,y=350)#设置随机角度旋转按钮
button8 = tkinter.Button(win,text="Random Rotation",command=Rotation)
button8.place(x=600,y=400)#设置padding按钮
button9 = tkinter.Button(win,text="Padding",command=padding)
button9.place(x=600,y=450)#设置随机灰度化按钮
button9 = tkinter.Button(win,text="Random Gray",command=random_gray)
button9.place(x=600,y=500)#设置亮度的按钮
button10 = tkinter.Button(win,text="Brightness",command=set_bright)
button10.place(x=600,y=550)#设置对比度的按钮
button11 = tkinter.Button(win,text="Contrast",command=set_contrast)
button11.place(x=600,y=600)#设置色度按钮
button12 = tkinter.Button(win,text="Hue",command=set_hue)
button12.place(x=600,y=650)#设置饱和度按钮
button13 = tkinter.Button(win,text="Saturation",command=set_saturation)
button13.place(x=600,y=700)#设置退出按钮
button0 = tkinter.Button(win,text="Exit",command=win.quit)
button0.place(x=600,y=750)
win.mainloop()

四、结果展示

1.总的界面

2.交互界面的使用

python实现一个简单的图像处理交互界面(tkinter库)相关推荐

  1. 【Python】如何用python做一个简单的输入输出交互界面?

    看到知乎上有人在问,如何使用Python做一个简单的输入输出交互界面? 交互界面就涉及到GUI编程. Python有很多GUI框架,功能大同小异. 其中比较出名的有「PyQT」.**wxPython. ...

  2. 如何用python做一个简单的输入输出交互界面?

    大家好,我是一行 想问下你写的程序怎么分享给别人使用? **直接发代码!**那不会代码的人岂不是得抓瞎 **那做成网站或者微信小程序!**时间成本太高了,更何况服务器又是一笔成本,后期可能还得不断维护 ...

  3. python实现一个简单的加法计算器_Python tkinter实现简单加法计算器代码实例

    tkinter 是 Python 的标准 GUI 库.Python 使用 tkinter 可以快速的创建 GUI 应用程序.由于 tkinter 是内置到 python 的安装包中.只要安装好 Pyt ...

  4. python编写一个简单计算器_如何用Tkinter写个简单计算器

    上机实践课程开始了,嗯,老师来了之后念了下PPT,然后说:开始做吧......... 然后就开始了Python的GUI之路,以前没接触过PYthon的可视化界面(虽然这样很不明智) 但是现在做起来感觉 ...

  5. python界面设计-手把手教你用Python设计一个简单的命令行界面

    原标题:手把手教你用Python设计一个简单的命令行界面 对 Python 程序来说,完备的命令行界面可以提升团队的工作效率,减少调用时可能碰到的困扰.今天,我们就来教大家如何设计功能完整的 Pyth ...

  6. php和python交互-Python如何实现简单的用户交互程序(示例)

    本篇文章给大家带来的内容是关于Python如何实现简单的用户交互程序(示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 我们经常使用的程序都会有与用户交互的程序,比如网页的登录, ...

  7. python编写一个登陆验证程序_用python实现一个简单的验证码

    我们经常在登录一个网站,或者注册的时候需要输入一个验证码,有时候觉得很烦,因为有些验证码不仅复杂还看不清,许多用户就会因为这些而懒得再登录或者注册之类的. 既然验证码会造成流失用户的风险,为什么大家都 ...

  8. 如何用Python制作一个简单的二维码生成器

    目录 前言 1.安装第三方库 2.QRCode参数解释 3.自定义二维码生成器 4.给二维码加图片 5.全部代码 6.结果 前言 二维码又称二维条码,常见的二维码为QR Code,QR全称Quick ...

  9. 用python做一个简单的游戏,用python写一个小游戏

    大家好,本文将围绕如何用python做一个简单的小游戏展开说明,python编写的入门简单小游戏是一个很多人都想弄明白的事情,想搞清楚用python做一个简单的游戏需要先了解以下几个事情. 1.Pyt ...

最新文章

  1. linkin大话面向对象--多态
  2. MAC ox下配置mysql
  3. linux 下 用户与用户组
  4. 机器学习入门学习笔记:(4.1)SVM算法
  5. SQLite 附加数据库(http://www.w3cschool.cc/sqlite/sqlite-attach-database.html)
  6. 深度学习:又一次推动AI梦想(Marr理论、语义鸿沟、视觉神经网络、神经形态学)
  7. C++之全局函数和成员函数的转换
  8. 文本分类的一种对抗训练方法
  9. [转]各种互斥量的总结
  10. sqoop各类命令示范
  11. raft算法_golang-raft算法理论与实践
  12. C#:Md5和Sha1两种加密方式
  13. java calendar数组_Java Calendar Date使用总结
  14. ScreenToGif录制录屏gif软件的推荐通用设置,优化使用体验
  15. Redis 增加互斥锁
  16. 如何快速提高idm下载速度?idm下载速度只有几十kb
  17. 软工学子带你一起学习工程经济学!献上与工作相关的实用公式(值得一进)
  18. 淘宝获取商品历史价格API
  19. 洛谷P1867 【Mc生存】经验值
  20. python解释器cpython的源码简要介绍

热门文章

  1. 入门图形学:平直着色和平滑着色
  2. Vue组件间通信之$emit/$on
  3. [CVE-2020-9496]Apache Ofbiz RCE
  4. springboot配置tomcat
  5. 哈哈,手把手教你撸一个在线网盘(附源码)!
  6. 人和人最大的差别不是技术上的高低,而是你的人生观价值观
  7. 什么是IOC/DI?
  8. 2000-2017年全球人口密度数据下载LandScan
  9. 电子科技大学 易查分网站 爬虫 批量爬取成绩
  10. mac系统免费软件 GoodNotes 5.8.6 中文版 (最好的手写笔记应用)