python下批量修改图片格式和大小

第一种方法用到opencv库
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('D:\\new\\ren\\test','D:\\new\\ren\\object')

第二种方法用到PIL库
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('D:\\new\\ren\\new','D:\\new\\ren\\new1')

你可能感兴趣的:(python下批量修改图片格式和大小)