3

[GIMP 2.8.16]


EDITED: See below.

Hi,

I am new to python, and I need to ask the community to please help me find where is this code failing. I am having trouble. This is version 1.0, following this answer:

import glob
source_folder = "/home/pecesaquadros/Desktop/T/"
dest_folder = "/home/pecesaquadros/Desktop/T2/"

def auto(source_folder, dest_folder):
    for filename in glob(source_folder  + "/*.JPG"):
        img = pdb.gimp_file_load(source_folder + filename, source_folder + filename)
        pdb.gimp_image_rotate(img,0)
        pdb.gimp_image_convert_grayscale(img)
        drawable = pdb.gimp_image_get_active_drawable(img)
        pdb.gimp_brightness_contrast(drawable, 28,100)
        disp = pdb.gimp_display_new(img)
        yield img
        pdb.gimp_image_merge_visible_layers(img, CLIP_TO_IMAGE)
        pdb.gimp_file_save(img, img.layers[0], dest_folder + filename, dest_folder + filename)
        pdb.gimp_display_delete(disp)
        pdb.gimp_image_delete(img)  # drops the image from gimp memory

seq = auto(source_folder, dest_folder)
next(seq)

with output, in GIMP 2.8.16 Python Console:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<input>", line 2, in auto
TypeError: 'module' object is not callable

Many thanks. I am sure this question will help many. I might make a plugin out of it, with interesting parameters, available for everybody; it will be named "Photocopy" :)

...next I am creating a loop to create a TeX code that will compile all images to a pdf...

Thanks again,

Al


EDIT

New code:

import glob
source_folder = "/home/pecesaquadros/Desktop/T/"
dest_folder = "/home/pecesaquadros/Desktop/T2/"

def auto(source_folder, dest_folder):
    for filename in glob.glob(source_folder  + "/*.JPG"): #blabla.JPG
        img = pdb.gimp_file_load(filename,filename)
        pdb.gimp_image_rotate(img,0)              #Editing starts
        pdb.gimp_image_convert_grayscale(img)
        pdb.gimp_image_select_rectangle(img, 2, 0, 0, 1839, 600)
        drawable = pdb.gimp_image_get_active_drawable(img)
        pdb.gimp_brightness_contrast(drawable, 60,127)
        pdb.gimp_image_select_rectangle(img, 2, 0, 600, 1839, 900)
        drawable = pdb.gimp_image_get_active_drawable(img)
        pdb.gimp_brightness_contrast(drawable, 32,127)
        pdb.gimp_image_select_rectangle(img, 2, 0, 1500, 1839, 900)
        drawable = pdb.gimp_image_get_active_drawable(img)
        pdb.gimp_brightness_contrast(drawable, 14,127)
        pdb.gimp_image_select_rectangle(img, 2, 0, 2400, 1839, 900)
        drawable = pdb.gimp_image_get_active_drawable(img)
        pdb.gimp_brightness_contrast(drawable, 0,127)
        disp = pdb.gimp_display_new(img)      #Editing ends
        yield img               #Image is displayed for me to manipulate
        pdb.gimp_image_merge_visible_layers(img, CLIP_TO_IMAGE)
        pdb.gimp_file_save(img, img.layers[0],filename,dest_folder + filename)
        pdb.gimp_display_delete(disp)
        pdb.gimp_image_delete(img)

seq = auto(source_folder, dest_folder)
next(seq) #This, and enter, to go to the next file

Results in:

  1. Files are overwritten, not saved in the new folder
  2. Sometimes, and only sometimes, one of these two errors appear:

A

GIMP Message

B

GIMP Message

0_0

PS: yield is in my plans.

  • you should be aware the code you copied from sports the yield statement because that question had a requirement for manual intervention in each image. If all you want to do in the image is automated, you can drop the yield line, as well as the calls to pdb.gimp_display_new and pdb.gimp_display_delete - and GIMP will process all your images as fast as possible. – jsbueno Jul 28 '17 at 0:53
  • I want yield to happen. Thank you jsbueno, and thank you for your email. – Álvaro F. López de Quadros Jul 28 '17 at 11:42
  • I can't get around with, as coded in your answer, pdb.gimp_file_save(img, img.layers[0],dest_folder + filename,dest_folder + filename). Output is RuntimeError: Could not open '/home/pecesaquadros/Desktop/T2//home/pecesaquadros/Desktop/T‌​2/P1050289.JPG' for reading: No such file or directory. I have fiddled around with it. For instance,pdb.gimp_file_save(img, img.layers[0],filename,dest_folder + filename) and pdb.gimp_file_save(img, img.layers[0],filename,filename) both let the program run but files are overwritten and not saved in dest_folder. – Álvaro F. López de Quadros Jul 29 '17 at 10:49
2

glob is module that contains a glob function, so that shouldn't be glob(source_folder + "/*.JPG") but glob.glob(source_folder + "/*.JPG")

Also, if you give a path as part of the pattern to glob.glob(), the output will contain the full path to the files. Since you are re-adding the directory name to it, it appears twice in the name you give to gimp_file_load(). So try something like:

for filename in glob.glob(source_folder  + "/*.JPG"):
    img = pdb.gimp_file_load(filename,filename)
  • Thank you. I've learned more than what I was expecting from such a concise answer. However, new output is: Traceback (most recent call last): File "<input>", line 1, in <module> File "<input>", line 3, in auto RuntimeError: Could not open '/home/pecesaquadros/Desktop/T//home/pecesaquadros/Desktop/T/P1050289.JPG' for reading: No such file or directory – Álvaro F. López de Quadros Jul 27 '17 at 21:11
  • See edited answer. – xenoid Jul 27 '17 at 21:54
  • Thank you, xenoid. Same problem occurred with pdb.gimp_file_save(img, img.layers[0], dest_folder + filename, dest_folder + filename). After fixing both, see what happens... (see edited question). – Álvaro F. López de Quadros Jul 28 '17 at 11:40
  • Your code sort of works for me. Add print statements in your code (especially to show the value of img and img). Then you can see which statement is producing the error message and what is the value of img.ID when this happens. – xenoid Jul 28 '17 at 13:58
  • I will investigate further. To finish with this and call it to an end (or to an answer), files are overwritten, not saved in the destination folder. – Álvaro F. López de Quadros Jul 28 '17 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.