0

My goal is to put one image over the other and save the resulting image as jpeg file. The first one is new one, it has 450x300 dimension and white background. The second one is loaded from file and its visible part is added as a layer to the first.

I have added the following python script to /usr/lib/gimp/2.0/plug-ins folder

#!/usr/bin/python
import os

from gimpfu import *
import gimpfu

import logging
def scale(timg, tdrawable, imageName):
    logger = logging.getLogger()
    logger.info('got logo file {0}'.format(imageName))

    newImage = pdb.gimp_image_new(450, 300, RGB)

    newLayer = pdb.gimp_layer_new(newImage, 450, 300, 0, "background", 100.0, NORMAL_MODE)

    pdb.gimp_context_set_background((255, 255, 255))
    pdb.gimp_drawable_fill(newLayer, gimpfu.BACKGROUND_FILL)
    logger.info('Created new background image {0}')
    newImage.add_layer(newLayer, 0)

    timg = pdb.gimp_file_load(imageName, imageName)

    tdraw = pdb.gimp_layer_new_from_visible(timg, newImage, 'logo')      
    newImage.add_layer(tdraw, 1)
    finalLayer = pdb.gimp_image_merge_down(newImage,tdraw,1)
    fileNameNoExt = os.path.splitext(imageName)[0]
    pdb.file_jpeg_save(newImage, finalLayer, fileNameNoExt + '.jpg', fileNameNoExt + '.jpg', 1.0, 0.0, 1, 1, '', 0, 0, 0, 0)

Running it via

gimp --no-interface -b '(python-fu-my RUN-NONINTERACTIVE 0 0 "logo.png")' -b '(gimp-quit 0)'

returns

(gimp:4224): GLib-CRITICAL **: g_error_new_literal: assertion 'domain != 0' failed Traceback (most recent call last): File "/usr/lib/gimp/2.0/python/gimpfu.py", line 821, in _run return apply(func, params[1:]) File "/usr/lib/gimp/2.0/plug-ins/my.py", line 31, in scale finalLayer = pdb.gimp_image_merge_down(newImage,tdraw,1) RuntimeError: execution error batch command experienced an execution error: Error: ( : 1) Procedure execution of python-fu-my failed

What is wrong? How to do it correctly and understand the root cause of the problem.

Edit. Following suggestions by xenoid : 1) Using gimp_image_merge_down results in logo.jpeg with white background of 450, 300 size

def my(timg, tdrawable, imageName):
    logger = logging.getLogger()
    logger.info('got logo file {0}'.format(imageName))

    newImage = pdb.gimp_image_new(450, 300, RGB)

    newLayer = pdb.gimp_layer_new(newImage, 450, 300, 0, "background", 100.0, NORMAL_MODE)

    pdb.gimp_context_set_background((255, 255, 255))
    # pdb.gimp_context_set_foreground((255, 255, 255))
    pdb.gimp_drawable_fill(newLayer, gimpfu.BACKGROUND_FILL)
    logger.info('Created new background image {0}')
    newImage.add_layer(newLayer, 0)

    logger.info('loading logo {0}'.format(imageName))
    logo = pdb.gimp_file_load(imageName, imageName)
    # logoDrawable = pdb.gimp_image_get_active_layer(logo)

    #
    visibleLayer = pdb.gimp_layer_new_from_visible(logo, newImage, 'logo')
    newImage.add_layer(visibleLayer, 1)
    mergedL = pdb.gimp_image_merge_down(newImage, newLayer, 0)

    fileNameNoExt = os.path.splitext(imageName)[0]
    pdb.file_jpeg_save(newImage, mergedL, fileNameNoExt + '.jpg', fileNameNoExt + '.jpg', 1.0, 0.0, 1, 1, '', 0, 0, 0, 0)

2) Using newImage.add_layer(visibleLayer, 0) results in original png image given as input stored in jpg format

def scale(logo, tdrawable, imageName):
    logger = logging.getLogger()
    logger.info('got logo file {0}'.format(imageName))

    newImage = pdb.gimp_image_new(450, 300, RGB)

    newLayer = pdb.gimp_layer_new(newImage, 450, 300, 0, "background", 100.0, NORMAL_MODE)

    pdb.gimp_context_set_background((255, 255, 255))
    # pdb.gimp_context_set_foreground((255, 255, 255))
    pdb.gimp_drawable_fill(newLayer, gimpfu.BACKGROUND_FILL)
    logger.info('Created new background image {0}')
    newImage.add_layer(newLayer, 0)

    logger.info('loading logo {0}'.format(imageName))
    logo = pdb.gimp_file_load(imageName, imageName)
    # logoDrawable = pdb.gimp_image_get_active_layer(logo)

    #
    visibleLayer = pdb.gimp_layer_new_from_visible(logo, newImage, 'logo')
    newImage.add_layer(visibleLayer, 0)
    # mergedL = pdb.gimp_image_merge_down(newImage, newLayer, 0)

    fileNameNoExt = os.path.splitext(imageName)[0]
    pdb.file_jpeg_save(newImage, visibleLayer, fileNameNoExt + '.jpg', fileNameNoExt + '.jpg', 1.0, 0.0, 1, 1, '', 0, 0, 0, 0)
rok
  • 9,403
  • 17
  • 70
  • 126
  • Stuff you write shouldn't go in /usr/lib/gimp/2.0/plug-ins, but in ~/.gimp-2.8/plug-ins (note the leading dot, tell the explore to show hidden files). Or you put it some more "visible" directory, and add that directory in `Edit>Preferences>Folders>Plug-ins`. You can also add in ~/.gimp-2.8/plug-ins a soft link to the actual location of the file. – xenoid May 05 '17 at 22:12

1 Answers1

1

This:

tdraw = pdb.gimp_layer_new_from_visible(timg, newImage, 'logo')      
newImage.add_layer(tdraw, 1)

Doesn't add the layer as the top one, but under the existing top one. If it's the second layer then it becomes the bottom one. And in that case merge_down has nothing to merge on.

xenoid
  • 8,396
  • 3
  • 23
  • 49
  • thanks, so how would you achieve the task correctly? – rok May 05 '17 at 23:45
  • 1
    Either apply merge_down to the other layer, or use `newImage.add_layer(tdraw, 0)` to add the layer as the top layer. – xenoid May 06 '17 at 13:49
  • Hi, thanks. I tried your suggestions with no success. Maybe, i miss smth. ( – rok May 27 '17 at 23:30
  • 1
    Yes, the the file-xxx-save() only saves the given drawable, so you have to flatten the image before saving. – xenoid May 28 '17 at 01:24