【问题标题】:Python Pillow library not saving files with same name in same locationPython Pillow 库不在同一位置保存同名文件
【发布时间】:2021-06-23 09:04:49
【问题描述】:

下面是我用来将二进制数据转换为图像然后保存的代码

img = base64.b64decode(rec.image3)
img_conv = Image.open(io.BytesIO(img))
img_format = img_conv.format
img_conv.save('{}/{}'.format(path, rec.image_name), format(img_format))

有 4 个图像具有相同的代码,我想处理如果所有文件名在同一位置相同的情况,它应该强制保存 4 个图像,即使它有重复的名称。

任何建议将不胜感激。谢谢。

【问题讨论】:

    标签: python-3.x image-processing python-imaging-library odoo file-handling


    【解决方案1】:

    假设您希望为每个文件保留不同的名称:只要您的目录中存在具有该名称的文件,就将“_”附加到原始文件名。

    from pathlib import Path
    
    path_to_save = Path(path, rec.image_name)
    
    while path_to_save.exists():
        path_to_save = Path(str(path_to_save) + '_')
        
    img_conv.save(path_to_save, format(img_format))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多