简体   繁体   English

Python PIL将图像保存在目录中,如果名称相同则不覆盖

[英]Python PIL save Image in directory no override if the name is same

I am trying to make a backup copy of an image, because it will be resized often. 我正在尝试制作图像的备份副本,因为它会经常调整大小。 I am asking for the path where the image is (Tkinter), then I am adding to the path and the image an "-original" and save it in the same directory where I got it from. 我要求输入图像的路径(Tkinter),然后在路径和图像上添加“原始”,并将其保存在从中获取图像的同一目录中。

The problem is everytime I use this function it overrides the original, because there is no loop which makes the programm check wheter there already exists a file with "-original". 问题是每次我使用此功能时,它都会覆盖原始功能,因为没有循环使程序检查是否已经存在带有“ -original”的文件。

Thats how I make the backup save: 那就是我如何保存备份:

        pfad = askopenfilename()
        im_backup = Image.open(pfad)
        start_string = pfad[:pfad.index(".")]
        ende_string = pfad[pfad.index("."):]
        im_backup.save(start_string + "-original" + ende_string)

Currently I am working on a solution with os which could work, but I have the feeling it has to be simple. 目前,我正在使用可以工作的os解决方案,但我觉得它必须很简单。 I red the documentation of PIL.Image.save, there are more arguments which can be passed in to save, but I dont figured out which one has to be used to prevent overriding. 我修改了PIL.Image.save的文档,可以传递更多的参数来保存,但是我不知道必须使用哪个参数来防止覆盖。

My current solution (not working yet) is checking with os.listdir(directory) wether there is already a (start_string + "-original" + ende_string) file and only save it there if it is false. 我当前的解决方案(尚无法使用)正在使用os.listdir(directory)进行检查,是否已经有一个(start_string +“ -original” + ende_string)文件,并且只有在它为false时才保存在那里。

Thanks in advance! 提前致谢!

Consider using os.path.splitxext instead of slicing and indexing. 考虑使用os.path.splitxext代替切片和索引。 You can also use os.path.isfile instead of listdir . 您也可以使用os.path.isfile代替listdir

import os
pfad = askopenfilename()
name, ext = os.path.splitext(pfad)
backup_name = name + "-original" + ext
if not os.path.isfile(backup_name):
    im_backup = Image.open(pfad)
    im_backup.save(backup_name)

It's probably bot possible to do what you want by passing something to .save() . 通过将某些内容传递给.save()机器人可能可以做您想要的事情。

The thing in the docs that probably made you think that it's possible is this: 文档中可能使您认为有可能的事情是:

  • file – File name or file object. file –文件名或文件对象。
  • format – Optional format override. format –可选的格式替代。 If omitted, the format to use is determined from the filename extension. 如果省略,则使用的格式由文件扩展名决定。 If a file object was used instead of a filename, this parameter should always be used. 如果使用文件对象而不是文件名,则应始终使用此参数。
  • options – Extra parameters to the image writer. options –图像编写器的额外参数。

But in the source code there are lines, 但是在源代码中有几行,

self.encoderinfo = params
self.encoderconfig = ()

in which all the options are stored in a variable that has relation to encoder, so it's probably not what you want. 其中所有选项都存储在与编码器相关的变量中,因此可能不是您想要的。

So, you should probably consider doing what @Kevin suggested. 因此,您可能应该考虑执行@Kevin建议的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM