Gimp-Forum.net
Whitlisting file inputs - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Whitlisting file inputs (/Thread-Whitlisting-file-inputs)



Whitlisting file inputs - radzo73 - 12-22-2019

Hey,
I have a script that takes all the files in a folder, crops them, and exports them. However, it tries to take things in that folder that aren't images (text files, pesky invisible metadata files) and tries to edit them, which throws an error. How do I fix that?


RE: Whitlisting file inputs - Ofnuts - 12-22-2019

Just skip files with a extension which isn't PNG, something like:

Code:
filename,fileext = os.path.splitext(element) # replaces a line of yours
if fileext[1:].upper() != 'PNG':
   continue

You can also more directly get the files you need:

Code:
import glob
files=glob.glob(os.path.join(file_inpath,'*.png')  # elements have their full paths

Unless I'm mistaken the file_mask is always the same, so this will always yield the same result:

Code:
# Add mask to new layer over image
layer_mask = pdb.gimp_file_load_layer(image,file_mask)
pdb.gimp_image_insert_layer(image,layer_mask,None,0)

# Mask layer alpha to selection
pdb.gimp_image_select_item(image,2,pdb.gimp_image_get_active_layer(image))
pdb.gimp_image_remove_layer(image,layer_mask)

# Invert Selection
pdb.gimp_selection_invert(image)

# Crop to selection
dimensions = pdb.gimp_selection_bounds(image)

So you can do it once for all, opening the mask as a separate image, that you discard once you have the values.


RE: Whitlisting file inputs - radzo73 - 12-22-2019

Thanks for the ideas!
However when you mentioned 
Code:
filename,fileext = os.path.splitext(element) # replaces a line of yours
if fileext[1:].upper() != 'PNG':
  continue
What line would it replace?

nvm, got it working. Again, thanks!