Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whitlisting file inputs
#1
Photo 
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?


Attached Files
.zip   batch-csp-cropper.py.zip (Size: 1.56 KB / Downloads: 543)
Reply
#2
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.
Reply
#3
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!
Reply


Forum Jump: