随笔-Python批量调整图片大小

随笔-Python批量调整图片大小,第1张

概述随笔-Python批量调整图片大小参考文档:https://pillow.readthedocs.io/en/stableeference/Image.html#PIL.Image.Image.resizehttps://pillow.readthedocs.io/en/stableeference/Image.html#PIL.Image.Image.savehttps://docs.python.org/zh-cn/3/library/os.html自 随笔-Python批量调整图片大小

参考文档:

https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.resizehttps://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.savehttps://docs.python.org/zh-cn/3/library/os.html

自己的数据集比较凌乱,图片大小不一,可以通过Python批量来调整图片大小为统一尺寸,方便训练。

import osfrom PIL import Imagedef resize(old_path, new_path, size, resample):    """      通过指定的resample方式调整old_path下所有的jpg图片为指定的size,并保存到new_path下     """    if os.path.isdir(old_path):        for child in os.Listdir(old_path):            if child.find('.jpg') > 0:                im = Image.open(old_path + child)                im_resized = im.resize(size=size, resample=resample)                if not os.path.exists(new_path):                    os.makedirs(new_path)                print(child, 'resize successfully!')                im_resized.save(new_path + child, im.format)            child_path = old_path + child + '/'            resize(child_path, new_path, size, resample)if __name__ == "__main__":    old_path = './test_dataset/'    new_path = './test_dataset_train/'    size = (1280, 720)    resample = Image.BIliNEAR # 使用线性插值法重采样    resize_BZ(old_path, new_path, size, resample)
总结

以上是内存溢出为你收集整理的随笔-Python批量调整图片大小全部内容,希望文章能够帮你解决随笔-Python批量调整图片大小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1189041.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存