深度学习voc数据集图片resize

本人新写的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 

posted on 2017-01-13 16:09  一生不可自决  阅读(9294)  评论(1编辑  收藏  举报

导航