Save every active layer of all opened images to JPG files in GIMP

I had to separate many images that had been scanned in groups of four or five images in a flatbed scanner. For that purpose I used "Divide Scanned Images" plugin (http://registry.gimp.org/node/22177) which makes that task automatically.

This plugin is not very well documented, at least I did not find a good one, only some articles in which the better advice is to increment "Selection threshold" parameter. The rest, by the "trial and error" method, though default values work well when an homogeneous path has been left between images with a color very different with respect to the ones inside the images.

But original scanned images are not always so good, or there is no time to modify them. In this situations is faster to crop each image manually; the problem here is that for many images, you have to spend a lot of time saving each image to disk. This can be avoided with a script that saves the active layer of all opened images. This is what I did in my first python script in GIMP.

The process could be the next:

1. You have an image similar to this and select the picture you want to separate in a new file with GIMP rectangular selection.


2. Copy the selection with Ctrl+V an create a new image from clipboard with Ctrl+Shift+V. The selection, and the creation of the new image, can be done once for each image you want to separate, and the same for many other images similar to the first.


3. All opened images (in memory) can be saved to disk with this simple script

https://drive.google.com/open?id=0B0Bm5coqKy3idXJFTV90VUx2cW8

which, once installed, can be accessed through File > Export JPG all.


Script

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Export all the current images to JPG.
# Mark all images clean, so GIMP won't warn you when you exit.
# Warning: if files already exist, they will be overwritten. Use with caution.

# GPL v2

from gimpfu import *
import gtk
import os
import collections
import urllib

entries = []

def python_export_jpg_all():

    #set prefix for name of all images (they will be saved with a sequential number)

    dialog = gtk.Dialog("Export all images to JPG", None, 0,
                        (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                         gtk.STOCK_OK, gtk.RESPONSE_OK))

    dialog.set_default_response(gtk.RESPONSE_OK) #marks the button visually

    table = gtk.Table(2, 2)
    table.set_row_spacings(10)
    table.set_col_spacings(10)

    entries.append(gtk.Label())
    entries[0].set_text("Prefix for all image names: ")
    entries.append(gtk.Entry())
    entries[1].set_text("img_")
    # entries[i].set_width_chars(10)
    # entries[i].connect("activate", ...)
    table.attach(entries[0], 0, 1, 0, 1)
    table.attach(entries[1], 1, 2, 0, 1)
    dialog.vbox.pack_start(table, True, True, 0)
    dialog.show_all()
    response = dialog.run()
    if response != gtk.RESPONSE_OK :
        return

    prefijo=entries[1].get_text()
    dialog.destroy()

    #ask for directory where images will be saved

    chooser = gtk.FileChooserDialog(title="Directory where images will be saved",
                                    action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                    buttons=(gtk.STOCK_CANCEL,
                                             gtk.RESPONSE_CANCEL,
                                             gtk.STOCK_OK,
                                             gtk.RESPONSE_OK))
    if chooser.run() != gtk.RESPONSE_OK:
        return
    ruta=urllib.unquote( chooser.get_uri() ).decode('utf8').replace("file://","")+os.path.sep
    chooser.destroy()

    #msg=gtk.MessageDialog(type=gtk.MESSAGE_INFO, buttons=gtk.BUTTONS_OK, message_format=None)
    #msg.set_markup(ruta)
    #msg.run()

    #export all open images

    image_array = gimp.image_list()
    i=0
    for image in image_array:
        nFichero=prefijo+str(i)+".jpg"
        pdb.file_jpeg_save(image, image.active_layer, ruta+nFichero, nFichero, 0.9, 0, 0, 0, "", 0, 0, 0, 0)
        pdb.gimp_image_clean_all(image)

        i+=1

register(
        "python_fu_export_jpg_all",
        "Export all current images, then mark them clean.",
        "Export all current images, then mark them clean.",
        "Juan Jose Alonso",
        "Juan Jose Alonso",
        "2016",
        "Export JPG all",
        "*",
        [
        ],
        [],
        python_export_jpg_all,
        menu = "<Image>/File/Save/"
)

main()


Installation of the script in GIMP:

1. Close GIMP
2. Download the former file
3. Copy the file inside GIMP:

cp ~/Downloads/export_jpg_all.py ~/.gimp-2.8/plug-ins/

4. Assign permissions

chmod +x ~/.gimp-2.8/plug-ins/export_jpg_all.py

Links:

http://gimpbook.com/scripting/slides30/gimpscripting.html
https://github.com/akkana/gimp-plugins
https://en.wikibooks.org/wiki/GIMP/Installing_Plugins
http://www.pygtk.org/pygtk2tutorial

Comentarios