在jpgfile初輸入自己需要處理的圖像的路徑
outdir輸入自己的需要保存處理好圖像的路徑
width, height分別代表寬和高
if filename.endswith('bmp'): 填寫自己圖像的格式
convertjpg(jpgfile, outdir, 100, 100) 輸入地址+輸出地址+寬+高
from PIL import Image
import os.path
import glob
jpgfile=' ./imgdata1'
outdir=' ./imgdata2'
def convertjpg(jpgfile, outdir, width, height):
for parent, dirnames, filenames in os.walk(jpgfile, followlinks=True):
for filename in filenames:
if filename.endswith('bmp'):
file_path = os.path.join(jpgfile, filename)
img = Image.open(file_path)
new_img = img.resize((width, height), Image.BILINEAR) ###核心代碼
new_img.save(os.path.join(outdir, filename))
if __name__ == "__main__":
convertjpg(jpgfile, outdir, 100, 100)