I want to add a variable text (file name) to a photo and fill the background with a color (white) so that the text is clearly visible.

How can I conveniently fill the already created text layer e.g. Danube-Iron-Gates(c) TEST with a colored e.g. white background?

I am using GIMP 2.10.2 (Windows10).

The following example code leads to the resulting image with text including a margin I attached here when using the python plug-in twice. The code has development level only.

#!/usr/bin/env python

# --------------------------------------------
# Add file name box
# --------------------------------------------
# Change Log
# ============================================
# Version 0.0.1 Initial release.

import math
import os
import os.path
import sys

from array import array 
from gimpfu import *

def format_pkz(sText):
    sConcatenatedText = sText + "(c) TEST"
    return sConcatenatedText

# -------------------------------------------------------------------------------------------
def add_filename_box(image, layer, use_filename, pkz1, placement):
    # set start of undo

    # Save the current foreground color:

    # Set foreground and background color to default
    gimp.set_background(255, 255, 255)

    # use filename for pkz1
    if use_filename:
        filename = image.filename
        filename_w_ext = os.path.basename(filename)
        file_name, file_extension = os.path.splitext(filename_w_ext) 
        pkz1 = format_pkz(file_name)

        if placement == 0:      # top right
            xOffset = image.width - 630
            yOffset = 50
        elif placement == 1:    # down right
            xOffset = image.width - 630
            yOffset = image.height - 150
        elif placement == 2:    # down left
            xOffset = 50
            yOffset = image.height - 150        
        else:                   # top left
            xOffset = 50
            yOffset = 50
        # create layer with text and add it to the image: image, drawable, x, y, text, border, antialias, size, size_type, fontname
        pdb.gimp_text_fontname(image, None, xOffset, yOffset, pkz1 , 10, True, 64, PIXELS, "Verdana Bold")   

    else:                       # don't use file name
        xOffset = 50
        yOffset = 200
        # create layer with text and add it to the image: image, drawable, x, y, text, border, antialias, size, size_type, fontname
        pdb.gimp_text_fontname(image, None, xOffset, yOffset, pkz1 , 10, True, 64, PIXELS, "Verdana Bold")         

    # Restore the old foreground color:
    # set end of undo

# -----------------------------------------------------------------------------
# register the plug-in
# ----------------------------------------------------------------------------- 

    "My function test.",
    "My function test for developing cases.",
    "help-info.de (c)",
    "<Image>/DEVELOP/Add filename box ...",
            (PF_TOGGLE, "use_filename", "PKZ aus Dateinamen ableiten", 1),
            (PF_STRING, "pkz1", "User Input", "0000-0-00000"),            
            (PF_OPTION, "placement", "Placement", 3, ("top right", "down right", "down left", "top left")),         


enter image description here


Several ways:

Plain bucket fill

The simpler is to bucket-fill the text layer in behind mode:

pdb.gimp_edit_bucket_fill(textlayer, BUCKET_FILL_FG, LAYER_MODE_BEHIND, 100, 0, False, 0, 0)

However the text layer has very little space on the sides, so the result can be unbalanced. A trick could be to pad your text with spaces at both ends, to extend the text layer

Use another layer (1)

You add a layer under the text layer (when doing so, you can chose its size/position), and bucket fill it with the required color

Use another layer (2)

You add another layer, filled with any color, then get an alpha-selection on the text layer (pdb.gimp_select_item(textlayer)) and then bucket-fill the selection on the added layer (and hide the text).

Completely different, and less obtrusive technique to ensure text visibility:

  • get a path from the text (path=pdb.gimp_vectors_new_from_text_layer(image, textlayer))
  • make a white outline by stroking the path in line mode with a 1px line:

    # Line mode for Stroke (new in 2.10)
    # You may need to use these three as well
    pdb.gimp_drawable_edit_stroke_item(drawable, path)

On a few difficult backgrounds:

enter image description here

| improve this answer | |

I can tell you a simple way to do it in GIMP, without scripting it. So, that may help you to script it.

  1. Create a text layer

  2. Discard the Text Information (this rasterizes the text)

  3. Remove the alpha channel

The transparent pixels in the text layer will be filled with the background colour selected. In this example the background colour selected is white:

enter image description here

| improve this answer | |
  • You do not need to remove the Text Information. This has the benefit of allowing for changes later, though at the price of either editing the text and losing the background or creating a new text layer from that info, and then redoing the background. – Michael Schumacher Jun 7 '18 at 13:43

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.