当前位置:   article > 正文

python实现一个简单的图像处理交互界面(tkinter库)_python怎么做图像处理的网页

python怎么做图像处理的网页

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

1.读取图片

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

2.随机比例缩放
new_im = transforms.Resize((100, 200))(im)
print(f'{im.size}---->{new_im.size}')
  • 1
  • 2
3.随机位置剪裁
new_im = transforms.RandomCrop(100)(im)   # 裁剪出100x100的区域
new_im = transforms.CenterCrop(100)(im)
  • 1
  • 2
4.随机水平/垂直翻转
new_im = transforms.RandomHorizontalFlip(p=1)(im)   # p表示概率
new_im = transforms.RandomVerticalFlip(p=1)(im)
  • 1
  • 2
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)
  • 1
  • 2
  • 3
  • 4
7.随机的灰度化
new_im = transforms.RandomGrayscale(p=0.5)(im) #以0.5的概率进行灰度化
  • 1
8.Padding(将原始图padding为正方形)
new_im = transforms.Pad((0, (im.size[0]-im.size[1])//2))(im)  # 原图为(500,313)
  • 1

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

1.创建一个界面窗口
win = tkinter.Tk()
win.title("picture process")
win.geometry("1280x1080")
  • 1
  • 2
  • 3
2.图像处理函数参考上面的实现
3.设置原图像和处理后的图像展示
render = ImageTk.PhotoImage(load)
img  = tkinter.Label(win,image=render)
img.image = render
img.place(x=100,y=100)
  • 1
  • 2
  • 3
  • 4
4.设置按钮(举一个为例子)
button2 = tkinter.Button(win,text="save",command=save)
button2.place(x=600,y=100)
  • 1
  • 2

三、代码实现

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 original
	original = load
	render = ImageTk.PhotoImage(load)
	
	img  = tkinter.Label(win,image=render)
	img.image = render
	img.place(x=100,y=100)
	
#随机比例缩放
def lessen():
	temp = original
	new_im = transforms.Resize((100,200))(temp)
	print(f'{temp.size}---->{new_im.size}')
	render = ImageTk.PhotoImage(new_im)
	global img2
	img2.destroy()
	img2  = tkinter.Label(win,image=render)
	img2.image = render
	img2.place(x=800,y=100)
	global save_img
	save_img = new_im
	
#随机位置裁剪
def random_cut():
	temp = original
	new_im = transforms.RandomCrop(100)(temp)
	render = ImageTk.PhotoImage(new_im)
	global img2
	img2.destroy()
	img2  = tkinter.Label(image=render)
	img2.image = render
	img2.place(x=800,y=100)
	global save_img
	save_img = new_im

#中心剪裁
def center_cut():
	temp = original
	new_im = transforms.CenterCrop(100)(temp)
	render = ImageTk.PhotoImage(new_im)
	global img2
	img2.destroy()
	img2  = tkinter.Label(image=render)
	img2.image = render
	img2.place(x=800,y=100)
	global save_img1
	save_img = new_im
	
#随机水平翻转
def Horizon():
	temp = original
	new_im = transforms.RandomHorizontalFlip(p=1)(temp)
	render = ImageTk.PhotoImage(new_im)
	global img2
	img2.destroy()
	img2  = tkinter.Label(image=render)
	img2.image = render
	img2.place(x=800,y=100)
	global save_img1
	save_img = new_im
	
#随机垂直翻转
def Vertical():
	temp = original
	new_im = transforms.RandomVerticalFlip(p=1)(temp)	
	render = ImageTk.PhotoImage(new_im)
	global img2
	img2.destroy()
	img2  = tkinter.Label(image=render)
	img2.image = render
	img2.place(x=800,y=100)
	global save_img1
	save_img = new_im
	
#随机角度旋转
def Rotation():
	temp = original
	new_im = transforms.RandomRotation(45)(temp)
	render = ImageTk.PhotoImage(new_im)
	global img2
	img2.destroy()
	img2  = tkinter.Label(image=render)
	img2.image = render
	img2.place(x=800,y=100)
	global save_img1
	save_img = new_im
	
#padding为正方形
def padding():
	temp = original
	new_im = transforms.Pad((0, (temp.size[0]-temp.size[1])//2))(temp)
	render = ImageTk.PhotoImage(new_im)
	global img2
	img2.destroy()
	img2  = tkinter.Label(win,image=render)
	img2.image = render
	img2.place(x=800,y=100)
	global save_img
	save_img = new_im

#随机灰度化
def random_gray():
	temp = original
	new_im = transforms.RandomGrayscale(p=0.5)(temp)
	render = ImageTk.PhotoImage(new_im)
	global img2
	img2.destroy()
	img2  = tkinter.Label(win,image=render)
	img2.image = render
	img2.place(x=800,y=100)
	global save_img
	save_img = new_im

#设置亮度
def set_bright():
	
	def show_bright(ev=None):
		temp = original
		new_im = transforms.ColorJitter(brightness=scale.get())(temp)
		render = ImageTk.PhotoImage(new_im)
		global img2
		img2.destroy()
		img2  = tkinter.Label(win,image=render)
		img2.image = render
		img2.place(x=800,y=100)
		global save_img
		save_img = new_im
		
	top = 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 = original
		new_im = transforms.ColorJitter(contrast=scale.get())(temp)
		render = ImageTk.PhotoImage(new_im)
		global img2
		img2.destroy()
		img2  = tkinter.Label(win,image=render)
		img2.image = render
		img2.place(x=800,y=100)
		global save_img
		save_img = new_im
		
	top = 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 = original
		new_im = transforms.ColorJitter(hue=scale.get())(temp)
		render = ImageTk.PhotoImage(new_im)
		global img2
		img2.destroy()
		img2  = tkinter.Label(win,image=render)
		img2.image = render
		img2.place(x=800,y=100)
		global save_img
		save_img = new_im
		
	top = 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 = original
		new_im = transforms.ColorJitter(saturation=scale.get())(temp)
		render = ImageTk.PhotoImage(new_im)
		global img2
		img2.destroy()
		img2  = tkinter.Label(win,image=render)
		img2.image = render
		img2.place(x=800,y=100)
		global save_img
		save_img = new_im
		
	top = 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 count
	count += 1
	save_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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306

四、结果展示

1.总的界面

01.png

2.交互界面的使用

02.png

12.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/554848
推荐阅读
相关标签
  

闽ICP备14008679号