Post Reply 

Basics of Paste Into

Jul 25, 2011, 06:13
Post: #1
Basics of Paste Into
Hi,

I wrote a nifty script that touches up Chroma Key effects in a series of video clips, but I recently ran in to an issue that I need to get cleared up to keep the project rolling.

Pretty basic actually, I just need to cut out a section of the image, do a few processes to what is left behind, then paste the previously cut area back in to the selection, a la the Paste Into Selection function of the GUI version.

Sounds basic but it is giving me trouble. I'm thrown by the verbiage on the Procedure Browser regarding the pasted selection going behind the selected area, etc.

Here's some code and some pseudo code as well:

(gimp-rect-select image 75 20 325 125 0 0 0 )
(gimp-edit-cut drawable)
Do a bunch of stuff to the drawable, including resetting the selection area
(gimp-rect-select image 75 20 325 125 0 0 0 )
here is where I want to paste the previously cut area back where I found it.

Thanks for pointing me in the right direction. Please let me know if I need to post additional code snippets etc.
Find all posts by this user
Quote this message in a reply
Jul 25, 2011, 06:51
Post: #2
RE: Basics of Paste Into
(Jul 25, 2011 06:13)longhaired1 Wrote:  Hi,

I wrote a nifty script that touches up Chroma Key effects in a series of video clips, but I recently ran in to an issue that I need to get cleared up to keep the project rolling.

Pretty basic actually, I just need to cut out a section of the image, do a few processes to what is left behind, then paste the previously cut area back in to the selection, a la the Paste Into Selection function of the GUI version.

Sounds basic but it is giving me trouble. I'm thrown by the verbiage on the Procedure Browser regarding the pasted selection going behind the selected area, etc.

Here's some code and some pseudo code as well:

(gimp-rect-select image 75 20 325 125 0 0 0 )
(gimp-edit-cut drawable)
Do a bunch of stuff to the drawable, including resetting the selection area
(gimp-rect-select image 75 20 325 125 0 0 0 )
here is where I want to paste the previously cut area back where I found it.

Thanks for pointing me in the right direction. Please let me know if I need to post additional code snippets etc.
The verbiage only says that the existing selection limits the effect of the paste (like it would limit the effect of painting...). However in your case I don't think the "into" is necessary, because:
  • When doing the operations manually, cut/copy followed by plain paste in the same image puts the pasted selection exactly where it comes from, in other words, cut+paste+anchor doesn't change anything.
  • Assuming you find the paste isn't in the right place, you can always move it with gimp-layer-translate.
However, I would not use the clipboard to hold precious data. In the Gimp way, you would duplicate the layer, use the selection to keep what has to be restored at the end, and merge it on the changed layer at the end of the processing. Possibly this also covers cases where a paste-into could get into trouble, like having a on-contiguous selection.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jul 25, 2011, 19:38
Post: #3
RE: Basics of Paste Into
Perfect solution to my issue. Thanks very much.
Find all posts by this user
Quote this message in a reply
Jul 25, 2011, 23:12
Post: #4
RE: Basics of Paste Into
Here is a follow up. Script is working fine when processing a folder of XCF files, but when I do the same for a folder of PNG files it barks at the Add Layer line:

When I use 2 as the layer ID I get this:
>>GIMP-Error: Calling error for procedure 'gimp-image-add-layer':
>>Item 'Background' (2) has already been added to an image

Anything else gives me this:

GIMP-Error: Calling error for procedure 'gimp-image-add-layer':
Procedure 'gimp-image-add-layer' has been called with an invalid ID for argument 'layer'. Most likely a plug-in is trying to work on a layer that doesn't exist any longer.

Here is the relevant snippet:

(gimp-image-crop image 469 380 180 70)
(gimp-layer-copy drawable 0)
(gimp-context-set-background '(20 43 77))
(gimp-by-color-select drawable '(20 43 77) 40 0 1 1 7 0)
(gimp-by-color-select drawable '(59 55 54) 15 0 1 1 7 0)
(gimp-by-color-select drawable '(20 30 47) 18 0 1 1 7 0)
(gimp-edit-clear drawable)
(gimp-selection-none image)
(gimp-image-add-layer image 2 2)
(gimp-layer-add-alpha 2)
(gimp-rect-select image 75 20 325 110 0 0 0 )
(gimp-edit-clear 2)
(gimp-image-merge-visible-layers image 0)
(set! drawable (car (gimp-image-get-active-layer image)))
(gimp-selection-none image)
(gimp-by-color-select drawable '(20 43 77) 15 0 1 1 0 0)
(gimp-selection-grow image 2)
(gimp-edit-clear drawable)

I would love to not convert 1500 png's to xcf and back again. Obviously I am missing something about setting ID layers.

Thanks for any help.
Find all posts by this user
Quote this message in a reply
Jul 26, 2011, 00:06
Post: #5
RE: Basics of Paste Into
I totally avoid this problem by using the Python-fu interface Smile (*) If you use the script-fu console, you should be able to issue PDB calls interactively and figure out what is different in a loaded PNG (implicit background added or else?). My <cough>-something years of programming taught me to avoid using hard-coded ids and this seems to also apply here Smile

(*) You also avoid other problems, and can write rather large programs/script that remain readable and maintainable, if only because you can use image/layers/paths et al. as objects with useful methods, and even the PDB calls take/return objects instead of IDs in most cases. And the python skills you develop can be used outside Gimp. End of blatant plug.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jul 26, 2011, 09:40 (This post was last modified: Jul 26, 2011 09:47 by paynekj.)
Post: #6
RE: Basics of Paste Into
I think you've misunderstood what gimp-image-add-layer is for. It's used to insert an already existing layer into an image, NOT to create a new layer (and hard-coding layer ids is a bad idea)

You would need to create the new layer using gimp-layer-new, gimp-layer-new-from-drawable, gimp-layer-new-from-visible, gimp-layer-copy etc

e.g.
Code:
(set! new-layer (car (gimp-layer-copy drawable TRUE)))
(gimp-image-add-layer image new-layer 0)

It's probably only working on your xcfs because they have multiple layers and therefore accidentally happen to have a layer with an id of 2

Kevin
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Is there a "paste multiple layers as images" plugin for GIMP? digiday 27 6,635 Sep 23, 2011 20:58
Last Post: digiday

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS