Mate de Vita
Mate de Vita

Reputation: 1325

GIMP Python Plugin to load 2 images as layers

I'm trying to make a plugin for gimp that opens two images as separate layers and transforms one of them (more on that below). I'm using GIMP 2.10.12.

I've been struggling to find a proper complete documentation for GIMP's Python interface and am mostly just working from what code snippets I've been able to find. This is what I have so far:

#!/usr/bin/env python2
import os

from gimpfu import *

def load_pair(img_f):
    mask_f = img_f.replace(IMG_DIR, PRED_DIR)
    result_f = os.path.splitext(img_f.replace(IMG_DIR, SAVE_DIR))[0]
    result_dir = os.path.dirname(result_f)
    if not os.path.isdir(result_dir):
        os.makedirs(result_dir)

    img = gimp.Image(100, 100)
    pdb.gimp_display_new(img)
    for f, name, pos in ((img_f, "Image", 0), (mask_f, "Mask", 1)):
        layer = pdb.gimp_file_load_layer(img, f)
        pdb.gimp_layer_set_name(layer, name)
        pdb.gimp_image_insert_layer(img, layer, None, pos)


register(
    "python_fu_open_image_pair",
    ...,
    "<Toolbox>/Image/Open Image Pair",
    "",
    [(PF_FILE, "img_f", "Image:", None)],
    [],
    load_pair
)

main()

This kind of does what I want but with a couple of problems.

Question 1

Currently I'm using gimp.Image(100, 100) to open a new image. This means I have to then Fit Canvas to Layers and adjust the zoom and position every time I load a new image pair.

Is there a way to find an image's size from pdb before opening it or do I have to use another library (like PIL) for this? I'm trying to keep my plugin's dependencies to a minimum.

The two images are guaranteed to have the same size.

Since File->Open automatically adjusts the canvas to the image size, I would hope there'd be a nice way to achieve this.

Question 2

I would like to automatically create and set the current working file to result_f + '.xcf' (see above code) - such that File -> Save would automatically save to this file. Is this possible in pdb?

Question 3

Most importantly, I currently have the Mask images saved as black-and-white images. Upon loading a mask as a new layer, I'd like to transform the black colour to transparent and white colour to green (0,255,0). Additionally, since they are saved as .jpg images, the white and black aren't necessarily exactly 255 and 0 intensities but can be off by a bit.

How do I do this automatically in my plugin?

Upvotes: 3

Views: 2018

Answers (1)

xenoid
xenoid

Reputation: 8904

  1. The good way would be to load the first image normally, and the rest as additional layers. Otherwise you can reset the canvas size (pdb.gimp_image_resize(...)) once you have loaded all the layers, and then create the Display.

  2. You can give a name and a default file to the image by setting image.name and image.filename.

  3. To convert the white to green use pdb.plug_in_colors_channel_mixer(...) and set all the gains to 0., except green in green. Make the black transparent use pdb.plug_in_colortoalpha(...).

PS: For color2alpha:

import gimpcolor

color=gimpcolor.RGB(0,255,0) # green, integer args: 0->255)
# or
color=gimpcolor.RGB(0.,1.,0) # green, floating point args (0.->1.)

pdb.plug_in_colortoalpha(image, layer, color)

The Python doc is a direct copy of the Scheme one. In Python, the RUN-INTERACTIVE parameter is not positional, so it doesn't appear in most calls, if you need it, it is a keyword parameter.

Upvotes: 3

Related Questions