Gimp-Forum.net
gimp-image-insert-layer: Item 'Background' (2) has already been added to an image - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: gimp-image-insert-layer: Item 'Background' (2) has already been added to an image (/Thread-gimp-image-insert-layer-Item-Background-2-has-already-been-added-to-an-image)



gimp-image-insert-layer: Item 'Background' (2) has already been added to an image - Skippy - 01-12-2019

Hi,

I am trying to write a script which would insert an image as a layer within layers of another image.  Here is what I got so far :

Code:
(define (foobar filename)
  (let* (
; open image to add
     (inputfile (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
     (picture (car (gimp-image-get-active-layer inputfile)))
; open file to insert image into
     (template (car (gimp-file-load RUN-NONINTERACTIVE "template.xcf" "template.xcf")))
    )
; add picture to template
    (gimp-image-insert-layer template picture 0 -1)
; flatten image
    (gimp-image-flatten template)
; couldn't get this line to work outside the next one, probably have to investigate "let" syntax
;    (drawable (car (gimp-image-get-active-drawable template)))
    (file-jpeg-save RUN-NONINTERACTIVE template (car (gimp-image-get-active-drawable template)) "outfile.jpg" "outfile.jpg" 0.95 0.95 1 1 "" 2 1 0 2)
     )
)


I call the script with gimp -i -b '(foobar "picture.jpg")' -b '(gimp-quit 0)'.

However it fails on gimp-image-insert-layer :


Quote:GIMP-Error: Calling error for procedure 'gimp-image-insert-layer':
Item 'Background' (2) has already been added to an image

batch command experienced an execution error:
Error: ( : 1) Procedure execution of gimp-image-insert-layer failed on invalid input arguments: Item 'Background' (2) has already been added to an image


I am struggling to understand this, the only results I get are two old threads on GF.c from which the useful replies have been removed…


What's wrong with my script ?


RE: gimp-image-insert-layer: Item 'Background' (2) has already been added to an image - rich2005 - 01-13-2019

Quote:..I am struggling to understand this, the only results I get are two old threads on GF.c from which the useful replies have been removed….

Bumped in case anyone knows those old posts.


RE: gimp-image-insert-layer: Item 'Background' (2) has already been added to an image - Ofnuts - 01-13-2019

I'm not fluent in script-fu (I code my scripts in Python, much easier/faster/powerful), but it looks like you are taking a reference to a layer and try to insert it in another image. This mean that this would be the same layer in both images...

You have to make an explicit copy of the layer, that you obtain with:
Code:
(gimp-layer-new-from-drawable yourSourceLayer  theImageWhereYouWantToInsertTheCopy)
and then insert that layer into your destination image:
Code:
(gimp-image-insert-layer theImageWhereYouWantToInsertTheCopy theLayerCopy parent position)



RE: gimp-image-insert-layer: Item 'Background' (2) has already been added to an image - Skippy - 01-13-2019

Thanks, I got it working with gimp-layer-new-from-drawable :


Code:
(define (foobar filename)
 (let* (
; open image to add
    (inputfile (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
    (picture (car (gimp-image-get-active-layer inputfile)))
; open file to insert image into
    (template (car (gimp-file-load RUN-NONINTERACTIVE "template.xcf" "template.xcf")))
; duplicate picture into template as new layer
    (layer (car (gimp-layer-new-from-drawable picture template)))
   )
; add picture to template
   (gimp-image-insert-layer template layer 0 -1)
; flatten image
   (gimp-image-flatten template)
   (file-jpeg-save RUN-NONINTERACTIVE template (car (gimp-image-get-active-drawable template)) "outfile.jpg" "outfile.jpg" 0.95 0.95 1 1 "" 2 1 0 2)
    )
)

I'll probably go for python later, but I didn't understood whether I had to install pygimp or pgimp or something and rather to investigate I figured it was a good opportunity to give a try to Lisp/Scheme.

PS : Ofnuts, I think you were actually (already) the good Samaritan in the two mentioned threads… Wink


RE: gimp-image-insert-layer: Item 'Background' (2) has already been added to an image - Ofnuts - 01-13-2019

In my son's university, they taught Scheme as the first language in Compsci 101. But not on its own merits. Their rationale is that nobody in their right mind will learn Lisp/Scheme on their own (unlike C/C+, Python...) so all students are on an equal standing since no-one is familiar with it. The students see it more as a form of hazing Smile

Yes, the Good Samaritan, seeing that a temple/forum merchant/owner was making money with his unpaid posts, removed them all (around 25% of the total).