本人新寫的3個pyhton腳本。
(1)單張圖片的resize:
1 # coding = utf-8 2 import Image 3 4 def convert(width,height): 5 im = Image.open("C:\\workspace\\PythonLearn1\\test.jpg") 6 out = im.resize((width, height),Image.ANTIALIAS) 7 out.save("C:\\workspace\\PythonLearn1\\test.jpg") 8 if __name__ == '__main__': 9 convert(256,256)
(2)resize整個文件夾里的圖片:
1 # coding = utf-8 2 import Image 3 import os 4 5 def convert(dir,width,height): 6 file_list = os.listdir(dir) 7 print(file_list) 8 for filename in file_list: 9 path = '' 10 path = dir+filename 11 im = Image.open(path) 12 out = im.resize((256,256),Image.ANTIALIAS) 13 print "%s has been resized!"%filename 14 out.save(path) 15 16 if __name__ == '__main__': 17 dir = raw_input('please input the operate dir:') 18 convert(dir,256,256)
注意點:服務器性能所限,要將500*500數據集resize到256*256。上面只是初步處理,實際上要訓練出高質量的模型以上的方式並不嚴謹,應當按比例resize,這樣的好處是圖片不會變形。
(3)按比例resize
1 # coding = utf-8 2 import Image 3 4 def convert(width,height): 5 im = Image.open("C:\\workspace\\PythonLearn1\\test_1.jpg") 6 (x, y)= im.size 7 x_s = width 8 y_s = y * x_s / x 9 out = im.resize((x_s, y_s), Image.ANTIALIAS) 10 out.save("C:\\workspace\\PythonLearn1\\test_1_out.jpg") 11 if __name__ == '__main__': 12 convert(256,256)
本來我的計划是按照比例resize圖片,因為圖片不可能正好是正方形的,所以想在不足256*256時用空白填充(這句話來自FCN的原文),后來有小伙伴說其實fcn可以接收任意尺寸大小的圖片,用空白填充可能還會引入噪聲,所以目前工作只做到這里。
關於python的圖像處理庫,PIL下面的鏈接給出了參考。在后續的制作數據集的過程中應該會有用武之地。
參考文章: http://blog.csdn.net/yupu56/article/details/50471119