统一修改文件下所有图片大小 Python3
Posted K同学啊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了统一修改文件下所有图片大小 Python3相关的知识,希望对你有一定的参考价值。
- 🔗 运行环境:python3
- 🚩 作者:K同学啊
- 🥇 选自专栏:《深度学习100例》
- 🔥 精选专栏:《新手入门深度学习》
- 📚 推荐专栏:《Matplotlib教程》
- 🧿 优秀专栏:《Python入门100题》
#提取目录下所有图片,更改尺寸后保存到另一目录
from PIL import Image
import os.path
import glob
folder_name = "./pos/"
target_name = "./test/"
def convertjpg(jpgfile,outdir,width=20,height=20):
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 os.listdir(folder_name):
print(folder_name+jpgfile)
convertjpg(folder_name+jpgfile,target_name)
以上是关于统一修改文件下所有图片大小 Python3的主要内容,如果未能解决你的问题,请参考以下文章