【问题标题】:Convert image format in series with Python使用 Python 串行转换图像格式
【发布时间】:2018-08-24 08:53:42
【问题描述】:

抱歉我的小问题,但我是 Python 新手。

我正在尝试将一系列JPEG 图像转换为BMP 格式并调整其大小。 我设法获得了单个图像的过程,但现在我无法自动化该过程,以便按顺序进行转换。 这是我的脚本

from PIL import Image
img = Image.open("C:/Users/***/Documents/images/1.jpg")
new_img = img.resize((320,240))
new_img.save("C:/Users/***/Documents/immages_bmp/1.bmp")

图像从 1 逐渐重命名为 10000。 有谁知道如何帮助我实现一个 for 循环来自动化这个过程? 非常感谢您的帮助

【问题讨论】:

    标签: python for-loop python-imaging-library jpeg bmp


    【解决方案1】:

    类似:

    from PIL import Image
    from glob import glob
    import os
    myDir = '/Users/me/pictures'
    pic_list = glob(myDir + os.sep + '*' + '.jpg')
    
    for pic in pic_list:
      #resize, use a string replace to name new bmps
      img = Image.open(pic)
      new_img = img.resize((320,240))
      newName = pic.replace(".jpg",".bmp")
      new_img.save(newName)
    

    应捕获所有图像,无论其命名约定如何,并且允许您在调整名称列表(或不调整大小)之前编辑名称列表。

    【讨论】:

    • 谢谢,抱歉,如果我想将新图像保存在不同的文件夹中,我应该怎么做?
    • 你的文件夹名称是否一致?类似于:newName = newName.replace("jpgFolder","bmpFolder")(就在首次分配 newName 之后)。
    • 并确保在运行程序之前已经创建了文件夹(目录)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2014-05-24
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多