msp
msp

Reputation: 59

GIMP script-fu (scheme) copy/paste from one image to another

I'm trying to do the simplest thing with GIMP Script-Fu, and I must be missing something basic.

I've made a 100x100 gray image, and a 200x200 black image, and I'm trying to copy the gray square onto the black square. The output however is just a black square (no gray square to be found). This is a minimal test-case of something more involved that I'm trying to do (involving 2 separate images), but I'll tackle one problem at a time.

Based on my own troubleshooting, it seems the problem might be in the copy/paste step, but unfortunately I can't quite understand the documentation for (gimp-edit-copy) and (gimp-edit-paste) in the Script-Fu Procedure Browser.

Where have I gone wrong?

Thanks

; 1) save this file as `$HOME/.gimp-2.8/scripts/copy-test.scm'
; 2) $ gimp -i -b '(copy-test)' -b '(gimp-quit 0)'
; 3) check `out.png' for result

(define (copy-test)
  (let* (; create 100x100 image img-a & 200x200 image img-b
         (img-a (car (gimp-image-new 100 100 RGB)))
         (img-b (car (gimp-image-new 200 200 RGB)))

         ; make new layers for each image
         (layer-a0 
           (car (gimp-layer-new 
                  img-a 100 100 RGB-IMAGE "a0" 100 NORMAL-MODE)))
         (layer-b0 
           (car (gimp-layer-new 
                  img-b 200 200 RGB-IMAGE "b0" 100 NORMAL-MODE))))

    ; insert layers into appropriate images
    (gimp-image-insert-layer img-a layer-a0 0 0)
    (gimp-image-insert-layer img-b layer-b0 0 0)

    (let (; get the drawables for each image
          (draw-a (car (gimp-image-get-active-layer img-a)))
          (draw-b (car (gimp-image-get-active-layer img-b))))

      ; change img-a from black to gray
      (plug-in-exchange 
        RUN-NONINTERACTIVE img-a draw-a 0 0 0 128 128 128 1 1 1)

      ; copy/paste a (100px gray) onto b (200px black)
      (gimp-edit-copy draw-a)
      (gimp-edit-paste draw-b TRUE)

      ; save output to result.png
      (gimp-file-save 
        RUN-NONINTERACTIVE img-b draw-b "out.png" "out.png"))))

UPDATE:

Following xenoid's suggestion, replacing

(gimp-edit-paste draw-b TRUE)

with

(gimp-floating-sel-anchor
  (car (gimp-edit-paste draw-b TRUE)))

fixed the problem.

Upvotes: 2

Views: 2241

Answers (2)

Gareth Thomas
Gareth Thomas

Reputation: 430

(define (copy-test)

(let* (; create 100x100 image img-a & 200x200 image img-b (img-a (car (gimp-image-new 100 100 RGB))) (img-b (car (gimp-image-new 200 200 RGB)))

     ; make new layers for each image
     (layer-a0 
       (car (gimp-layer-new 
              img-a 100 100 RGB-IMAGE "a0" 100 NORMAL-MODE)))
     (layer-b0 
       (car (gimp-layer-new 
              img-b 200 200 RGB-IMAGE "b0" 100 NORMAL-MODE))))

; insert layers into appropriate images
(gimp-image-insert-layer img-a layer-a0 0 0)
(gimp-image-insert-layer img-b layer-b0 0 0)

(let (; get the drawables for each image
      (draw-a (car (gimp-image-get-active-layer img-a)))
      (draw-b (car (gimp-image-get-active-layer img-b))))

  ; change img-a from black to gray
  (plug-in-exchange 
    RUN-NONINTERACTIVE img-a draw-a 0 0 0 128 128 128 1 1 1)

  ; copy/paste a (100px gray) onto b (200px black)
  (gimp-edit-copy draw-a)
  (gimp-floating-sel-anchor (car (gimp-edit-paste draw-b TRUE)))

  ; save output to result.png
  (gimp-file-save 
    RUN-NONINTERACTIVE img-b draw-b "out.png" "out.png"))))

Upvotes: -1

xenoid
xenoid

Reputation: 8904

As far as I can tell, your gimp-edit-paste creates a floating selection (some kind of temporary layer) but this doesn’t change draw-b until you "anchor" that floating selection (gimp-floating-sel-anchor). Then, gimp-file-save only saves the layer and not the whole image, so your paste is not included in the file.

When you create images in your scripts, it is useful (at least while debugging) to associate them to a "display" (gimp-display-new), which makes many such problems a lot more obvious).

Upvotes: 2

Related Questions