Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie: Python script error
#1
Question 
I am very new to the Python scripting in Gimp. I have used Gimp in the past for manipulating individual images, but now I have to run specific operations on hundreds of images. So I checked into the scripting options. I discarded Script-fu, as I am familiar with programming languages like C, C++, etc., but the Schemes thing is quite different. Python, on the other hand is similar enough.

So, to start out I looked at some sample scripts like this one:

Code:
import glob
source_folder = "c:/mysource_image_folder/" #type your folder inside the quotes - append the final "/"
dest_folder = "c:/destination_folder/"
for filename in glob.glob(source_folder  + "/*.png"):
   img = pdb.gimp_file_load(source_folder + filename, source_folder + filename)
   pdb.gimp_drawable_brightness_contrast(img.layers[0], 0.2, 0)
   pdb.gimp_file_save(img, img.layers[0], dest_folder + filename, dest_folder + filename)
   pdb.gimp_image_delete(img)  # drops the image from gimp memory

When I copy and paste that into the Python window, I get an error right away on the "import glob" line (I also get that error when I just type in "import glob"):

import glob
  File "<input>", line 1
    rt glob
          ^
SyntaxError: invalid syntax

So, obviously I am doing something wrong. Did I not install something right? I installed Python 3.7 (Win10), then reinstalled Gimp 2.10.20. Do I need to do something else?

And apologies if this is the wrong thread. Please point me to the right thread if so.

Thanks.
Reply
#2
Can't see anything wrong in the code. Could be something before it, that, combine with the "import glob", makes a syntax error. Try putting a line comment, or some meaningless statement first o see f the syntax error moves.

This said, Gimp uses Python v2 (2.7.22 IIRC) and comes with its own Python runtime on Windows. Installing another Python could break Gimp's Python (you may have to install Gimp after you install your other python).

If you are only doing simple like brightness/contrast, use ImageMagick, it is designed to be used in scripts. See http://www.imagemagick.org/Usage/color_mods/#level
Reply


Forum Jump: