Reputation: 23
I use GIMP for some batch editing, part of this is exporting painting with the filename taken from original image "filename" with this code:
pdb.file_png_save_defaults(ima, drawable, fullpath, filename)
At this moment, I have
fullpath=filename
so it saves the image into same folder as the source and filename and fullpath are identical.
What I want to do is to put it into subdirectory in this folder, lets call it "subfolder"
. But if I try:
fullpath = '\subfolder\'+filename
I get an error, obviously, because I am working with Python (or any programming language) for like half an hour and I hardly know what I am doing. Does anybody know how to achieve exporting images into a subfolder?
UPDATE:
Now it looks like this
sourcename = pdb.gimp_image_get_filename(ima)
basedir = os.path.dirname(sourcename)
if os.path.isdir(os.path.join(basedir,'Subfolder')) is False:
os.makedirs(os.path.join(basedir,'Subfolder'))
fullpath = os.path.join(basedir,'Subfolder',filename)
... and it works well. Almost. Now I am facing the problem with diacritics in basedir. When basedir contains something like "C:\Úklid\" I get "no such file or directory" error when code is creating Subdirecotry in it. After I rename the source folder to "C:\Uklid\" it works with ease. But I do need it to work with every path valid in Windows OS. Can someone help me with that?
UPDATE 2:
Looks like unicode() did the trick:
sourcename = pdb.gimp_image_get_filename(ima)
basedir = os.path.dirname(sourcename)
if os.path.isdir(os.path.join(basedir,'Subfolder')) is False:
os.makedirs(unicode(os.path.join(basedir,'Subfolder')))
fullpath = os.path.join(basedir,'Subfolder',filename)
Upvotes: 2
Views: 676
Reputation: 169494
Try this out:
import os
sourcename = pdb.gimp_image_get_filename(ima) # gets full name of source file
basedir= os.path.dirname(sourcename) # gets path
name = pdb.gimp_layer_get_name(ima.layers[0]) # gets name of active layer
filename = name+'.png'
fullpath = os.path.join(basedir,'subfolder',filename) # use os.path.join to build paths
# new line vvv
os.makedirs(os.path.join(basedir,'subfolder')) # make directory if it doesn't exist
drawable = pdb.gimp_image_active_drawable(ima)
pdb.file_png_save_defaults(ima, drawable, fullpath, filename) # saves output file
Upvotes: 1