本文實例講述了Python挑選文件夾里寬大于300圖片的方法。分享給大家供大家參考。具體分析如下:
這段代碼需要用到PIL庫。代碼如下所示:
- import sys
- import os
- sys.path.append('PIL')
- from PIL import Image as im
- path = '/home/hualun/桌面/img/'
- new_path = '/home/hualun/桌面/img2/'
- for x in os.listdir(path):
- if x.endswith('.jpg'):
- file = im.open(path+x)
- width = file.size[0]
- if width > 300:
- os.rename(path+x, new_path+x)
- print "%s 移動完成.." % x
希望本文所述對大家的Python程序設計有所幫助。