将文件夹内所有图像缩放至原来的五分之一
import os.path
import glob
import cv2
def convertjpg(jpgfile,outdir): #设置图像缩放规格
try:
img = cv2.imread(jpgfile)
new_img = cv2.resize(img,None,fx=0.2, fy=0.2, interpolation = cv2.INTER_CUBIC)
cv2.imwrite(os.path.join(outdir,os.path.basename(jpgfile)), new_img)
except Exception as e:
print(e)
for jpgfile in glob.glob("E:/pyexe/M/*.jpg"): #待转换的文件路径中所有.jpg文件
convertjpg(jpgfile,"E:/pyexe/M") #转换格式后的文件存储路径
将文件夹内的所有图像更改为指定的尺寸
from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir,width=600,height=450): #设置图像尺寸
img=Image.open(jpgfile)
try:
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
except Exception as e:
print(e)
for jpgfile in glob.glob("E:/pyexe/test/*.jpg"): #待转换的文件路径中所有.jpg文件
convertjpg(jpgfile,"E:/pyexe/test/test1") #转换格式后的文件存储路径