简体   繁体   中英

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.

Note: I am using 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.

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. You should double-check Gimp's interface to see if that's possible.

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.

If you want to do it in 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()
  • You can pass all the files to Gimp in one call so something like:
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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