本文实例讲述了Python实现批量修改图片格式和大小的方法。分享给大家供大家参考,具体如下:
第一种方法用到opencv库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import os import time import cv2 def alter(path, object ): result = [] s = os.listdir(path) count = 1 for i in s: document = os.path.join(path,i) img = cv2.imread(document) img = cv2.resize(img, ( 20 , 20 )) listStr = [ str ( int (time.time())), str (count)] fileName = ''.join(listStr) cv2.imwrite( object + os.sep + '%s.jpg' % fileName, img) count = count + 1 alter( 'C:\\imgDemo' , 'C:\\imgDemo1' ) |
第二种方法用到PIL库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import os import time from PIL import Image def alter(path, object ): s = os.listdir(path) count = 1 for i in s: document = os.path.join(path,i) img = Image. open (document) out = img.resize(( 20 , 20 )) listStr = [ str ( int (time.time())), str (count)] fileName = ''.join(listStr) out.save( object + os.sep + '%s.jpg' % fileName) count = count + 1 alter( 'C:\\imgDemo' , 'C:\\imgDemo1' ) |
运行上述代码可得到C:\imgDemo目录下对应批量生成的20*20大小的图片。
运行效果如下:
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/darkfour/article/details/77369465