简体   繁体   English

使用 Python 打开 gimp 文件夹中的所有图像

[英]Open all images in a folder on gimp using Python

I get something similar using:我得到类似的东西:

gimp = r'C:\Program Files\GIMP 2\bin\gimp-2.10.exe'

os.chdir(folder)

filesnames= os.listdir()

for f in filesnames:
    actfile = folder+'\\'+f
    p = subprocess.Popen([gimpcamin, actfile])
    p.wait()

But this script can open only one image and the next image opens when I close Gimp.但是这个脚本只能打开一个图像,当我关闭 Gimp 时会打开下一个图像。

Note: I am using VSCode注意:我正在使用 VSCode

I believe that the issue is that you have p.wait() immediately after p = subprocess.Popen([gimpcamin, actfile]) , which makes it wait for that command (ie, Gimp) to return before proceeding.我认为问题在于您在p = subprocess.Popen([gimpcamin, actfile])之后立即有p.wait() ,这使得它在继续之前等待该命令(即 Gimp)返回。

You should try:你应该试试:

gimp = r'C:\Program Files\GIMP 2\bin\gimp-2.10.exe'

os.chdir(folder)

filesnames= os.listdir()

processes = []
for f in filesnames:
    actfile = folder+'\\'+f
    processes.append(subprocess.Popen([gimpcamin, actfile]))

# wait for all processes to finish
for p in processes:
    p.wait()

I'm not sure that this what you really want.我不确定这是你真正想要的。 I suspect you rather have just another Window of Gimp open for each image, rather than opening a whole different instance.我怀疑您宁愿为每个图像打开另一个 Gimp 窗口,而不是打开一个完全不同的实例。 You should double-check Gimp's interface to see if that's possible.您应该仔细检查 Gimp 的界面,看看是否可行。

If this is all you do, you can just use gimp /path/dir/*.png in your shell, like most Unix utilities, the gimp command accepts several files.如果这就是你所做的一切,你可以在你的 shell 中使用gimp /path/dir/*.png ,就像大多数 Unix 实用程序一样, gimp命令接受几个文件。

If you want to do it in Python:如果您想在 Python 中执行此操作:

  • Since you do a chdir(), Gimp's current directory when you call it is the directory to which you moved, so you can use the path-less names obtained with listdir()由于你做了一个 chdir(),当你调用它时 Gimp 的当前目录就是你移动到的目录,所以你可以使用通过 listdir() 获得的无路径名称
  • You can pass all the files to Gimp in one call so something like:您可以在一次调用中将所有文件传递给 Gimp,例如:
gimpCmd=[r'C:\Program Files\GIMP 2\bin\gimp-2.10.exe','-n'] # -n to get a new Gimp instance, if necessary
subprocess.Popen(gimpCmd+os.listdir())

Of course this is possibly limited by the acceptable command line length, but in practice you'll be out of RAM (and display real estate) before you hit that limit.当然,这可能会受到可接受的命令行长度的限制,但实际上,在达到该限制之前,您将用完 RAM(并显示不动产)。

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

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