Gimptalk - Premier Gimp Community: batch cut-and-paste script? - Gimptalk - Premier Gimp Community

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

batch cut-and-paste script?

#1 User is offline   janmartin 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 07-July 10

Posted 19 July 2010 - 09:29 AM

Hi all,

I have the need for a batch cut-and-paste script.

There are 3 images. In each image there are 3 regions I need to copy-and-past one-by-one into a 4th image at a given location.
Sometimes the copy needs to be rotated by 180 degrees.
3 regions will be different in all 3 images.
New image size, region size and the position to paste into will be hard-coded in the script and never change.

3 source images:
Posted Image
Sketched result:
http://bit.ly/d31NuG

Images for testing (also available as a .zip):
http://bit.ly/9lYTxT

Any chance for this? I searched and couldn't find anything to adapt.

Thanks,
Jan

Pseudo code:

Create new image 9000 x 6000 named 123

Open image 1
Copy selection 0,0 to 3000,6000
paste into image 123 at 0,0
Copy selection 3000,0 to 6000,6000
paste into image 123 at 3000,0
rotate 180 degrees
Copy selection 6000,0 to 9000,6000
paste into image 123 at 6000,0
Close image 1

Open image 2
Copy selection 0,0 to 3000,6000
paste into image 123 at 0,0
rotate 180 degrees
Copy selection 3000,0 to 6000,6000
paste into image 123 at 3000,0
Copy selection 6000,0 to 9000,6000
paste into image 123 at 6000,0
rotate 180 degrees
Close image 2

Open image 3
Copy selection 0,0 to 3000,6000
paste into image 123 at 0,0
Copy selection 3000,0 to 6000,6000
paste into image 123 at 3000,0
rotate 180 degrees
Copy selection 6000,0 to 9000,6000
paste into image 123 at 6000,0
Close image 3

rotate image 123 90 deg counter-clock-wise.
Save image 123 at home/me/folder as jpeg with 80% quality.
0

#2 User is offline   paynekj 

  • Member
  • PipPip
  • Group: Members
  • Posts: 317
  • Joined: 01-June 05
  • LocationUK

Posted 19 July 2010 - 01:02 PM

Just to clarify, partly because your pseudo code doesn't look quite the same as what you've described and I'm trying to get some idea of what inputs you're expecting to supply.

You open the first image which contains three sub images. This requires to know the file name.

Copy each area from the source image - the size and coordinates of which are not fixed, so how is the decision made which area is needed?

Possibly have to rotate the area - again how is the decision about rotation made - is the centre image of the three always going to require a 180 degree rotation?

Pasting into the final image:
Your example photo shows the individual images overlapping and their positions aligned to get a reasonably level horizon. Do you require this to be automated?

I'm not wishing to be negative about the idea of automation, but at the moment it's looking like there are a lot of manual decisions needed.
0

#3 User is offline   janmartin 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 07-July 10

Posted 19 July 2010 - 01:23 PM

Hello paynekj,

thanks for looking into this.

Jan

paynekj said:

Just to clarify, partly because your pseudo code doesn't look quite the same as what you've described and I'm trying to get some idea of what inputs you're expecting to supply.

You open the first image which contains three sub images. This requires to know the file name.

You are right, the names of the 3 input files and 1 output file need to be passed to the script.

Quote

Copy each area from the source image - the size and coordinates of which are not fixed, so how is the decision made which area is needed?

There are 3 images with 3 frames. For each of the 3 images 3 sets of frame coordinates will be hard-coded in the script.

Quote

Possibly have to rotate the area - again how is the decision about rotation made - is the centre image of the three always going to require a 180 degree rotation?

It's always the same region that needs to be rotated. Different for 3 images.

Quote

Pasting into the final image:
Your example photo shows the individual images overlapping and their positions aligned to get a reasonably level horizon. Do you require this to be automated?

No. I will edit the script and enter the target coordinates once only.

Quote

I'm not wishing to be negative about the idea of automation, but at the moment it's looking like there are a lot of manual decisions needed.

Not at all.
Only the 4 filenames change. Everything else always stays the same.
0

#4 User is offline   janmartin 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 07-July 10

Posted 19 July 2010 - 07:06 PM

Hi,

just to let you know that I added a thumbnail of the 3 source images to my original posting at:

batch-cut-and-paste-script--p366511.html#p366511

Source:
http://bit.ly/8ZsxSM

Target:
http://bit.ly/d31NuG

What region to copy exactly and to what coordinates to paste will be hard-coded in the script.
Just use dummy figures.

Or download a few source images:
http://bit.ly/clFAPo

Jan
0

#5 User is offline   paynekj 

  • Member
  • PipPip
  • Group: Members
  • Posts: 317
  • Joined: 01-June 05
  • LocationUK

Posted 19 July 2010 - 09:09 PM

So here's a rough and ready interactive script that attempts to do some of what you've asked.

The numbers I've used come from my having shrunk your images down to 800 pixels high, just to make it quicker to try. I haven't saved the image as a JPG, but as an XCF just so I can leave the individual images as layers for manual alignment. Hopefully you can use it as a basis for something better.

Any suggestions about improvements/coding style/mistakes etc. would be welcome, as would any questions.

Kevin

; y-values to place the individual images at
(define kp24_destinationY '#(0 268 536 1072 804 1340 1608 1876 2144)) ; these values from having source images 800 pixels high

(define (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable positionIndex rotation imageIndex)
  (let*
    (
      (originX 0)
      (originY (vector 0 268 536 (- (car (gimp-image-height sourceImage)) 1)))  ; values for copying regions in the source images
      
      (originX_a 0) (originY_a 0) 
      (width1 356) ; (width1 2592)                     ; how wide the source images are
      
      (floatSelection 0) (activeLayer 0) (activeLayerX 0) (activeLayerY 0)
           
      (y0 0) (y1 0)
      (layerName (string-append "" (number->string imageIndex)))
    )
    (set! y0 (vector-ref originY positionIndex))
    (set! y1 (- (vector-ref originY (+ positionIndex 1)) y0))
    (gimp-rect-select sourceImage originX y0 width1 y1 CHANNEL-OP-REPLACE FALSE 0)
    (gimp-edit-copy sourceDrawable)

    (set! floatSelection (car (gimp-edit-paste targetDrawable TRUE)))
    
    (if (= rotation 180)
      (gimp-drawable-transform-rotate-simple floatSelection ROTATE-180 TRUE 0 0 FALSE)
    )
    
    (gimp-floating-sel-to-layer floatSelection)
    (set! activeLayer (car (gimp-image-get-active-layer targetImage)))
    (gimp-layer-set-offsets activeLayer 0 (vector-ref kp24_destinationY imageIndex))
    (gimp-layer-set-name activeLayer layerName)
  )
)

(define (kp24_mix_and_match filepath1 filepath2 filepath3 targetFilename)
  (let*
    (
      (targetWidth 356) (targetHeight (* 3 800))    ; size for the target image (before rotating)     
      (sourceImage 0) (sourceDrawable 0) (targetImage 0) (targetDrawable 0) 
    )
    (if (file-exists? filepath1) () (error "File not found:" filepath1) )  ; error checking
    (if (file-exists? filepath2) () (error "File not found:" filepath2) )
    (if (file-exists? filepath3) () (error "File not found:" filepath3) )

    (set! targetImage (car (gimp-image-new targetWidth targetHeight RGB))) ; create the target image
    (gimp-image-set-filename targetImage targetFilename)                   ; set the filename of the target image
    
    
    ; create a layer in the target image - could be deleted later in not wanted
    (set! targetDrawable (car (gimp-layer-new targetImage (car (gimp-image-width targetImage)) (car (gimp-image-height targetImage)) RGBA-IMAGE "Background" 100 NORMAL)))
    (gimp-image-add-layer targetImage targetDrawable 0)
    
    
    ; load a source image
    (set! sourceImage (car (gimp-file-load RUN-NONINTERACTIVE filepath1 filepath1)))
    (set! sourceDrawable (car (gimp-image-get-active-drawable sourceImage)))
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 0   0 2)
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 1 180 1)
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 2   0 0)
    (gimp-image-delete sourceImage)

    ; load a source image
    (set! sourceImage (car (gimp-file-load RUN-NONINTERACTIVE filepath2 filepath2)))
    (set! sourceDrawable (car (gimp-image-get-active-drawable sourceImage)))
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 0 180 5)
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 1   0 3)
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 2 180 4)
    (gimp-image-delete sourceImage)

    ; load a source image
    (set! sourceImage (car (gimp-file-load RUN-NONINTERACTIVE filepath3 filepath3)))
    (set! sourceDrawable (car (gimp-image-get-active-drawable sourceImage)))
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 0   0 8)
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 1 180 7)
    (kp24_copySelectionToNew sourceImage sourceDrawable targetImage targetDrawable 2   0 6)
    (gimp-image-delete sourceImage)

    (gimp-image-rotate targetImage ROTATE-270) ; rotate the target image
    (gimp-xcf-save RUN-NONINTERACTIVE  targetImage targetDrawable targetFilename targetFilename) ; save it as an xcf

; this is here just so I can see what's going on
;    (gimp-display-new sourceImage)
    (gimp-display-new targetImage)
    (gimp-displays-flush)
  )
)

(script-fu-register "kp24_mix_and_match"
		    _"/contributed/Interactive Mix and Match..."
		    "Interactive front end to batch shrink"
		    "I did this"
		    "GPL License"
		    "05/11/2009"
		    "*"
                    SF-FILENAME   _"File 1" "C:\\Kevin\\temp\\1278549116_077833-1_sml.jpg"
                    SF-FILENAME   _"File 2" "C:\\Kevin\\temp\\1278549116_077833-2_sml.jpg"
                    SF-FILENAME   _"File 3" "C:\\Kevin\\temp\\1278549116_077833-3_sml.jpg"
                    SF-STRING   _"Output File" "fred.xcf"
)


Here's what it made:
Posted Image
0

#6 User is offline   paynekj 

  • Member
  • PipPip
  • Group: Members
  • Posts: 317
  • Joined: 01-June 05
  • LocationUK

Posted 22 July 2010 - 09:40 AM

@janmartin

You PM'ed me asking how to run this from the command line on Linux:
gimp -i -f -d --verbose -b "(kp24_mix_and_match \"file1.jpg\" \"file2.jpg\" \"file3.jpg\" \"fileout.xcf\")" -b "(gimp-quit 0)"

And on windows I wrote a .bat file that has this in it:
"C:\Program Files\GIMP 2\bin\gimp-2.6.exe" -i -f -d --verbose -b "(kp24_mix_and_match \"C:\\Temp\\file1.jpg\" \"C:\\Temp\\file2.jpg\" \"C:\\Temp\\file3.jpg\" \"C:\\Temp\\fileout.xcf\")" -b "(gimp-quit 0)"


BUT first you need to modify the script I previously posted, as it has a couple of things that don't work in batch mode.

after the line
   (gimp-xcf-save RUN-NONINTERACTIVE  targetImage targetDrawable targetFilename targetFilename) ; save it as an xcf

Add in
(gimp-image-delete targetImage)


And comment out, or delete the lines
    (gimp-display-new targetImage)
    (gimp-displays-flush)


Having said that, I have been continuing to play, and I came up with a method of keeping all the potentially variable numbers in a separate configuration file so that that config file can be kept with the images. Is this useful to you? And should I carry on and get all the variable things into the config file?

Example of the config file:
;
; ALL THESE VALUES BASED ON SOURCE IMAGES 356x800 pixels
;
(sourceWidth 356)  ; how wide ALL the source images are
(sourceOriginX '#(0 0   0   0  )) ; the X position of the corners of each image in the source pictures
(sourceOriginY '#(0 268 536 799)) ; the Y position of the corners of each image in the source pictures

(targetWidth 356)   ; size of target image (before rotation)
(targetHeight 2400) 

; These are the coordinates for the destinations of the source images in the target image
(destinationX '#(0 0   0   0    0   0    0    0    0   ))
(destinationY '#(0 268 536 1072 804 1340 1608 1876 2144))


Kevin
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic