Page 1 of 2

Copy and paste regions

Posted: 2010-09-28T09:40:36-07:00
by galv
Is there a way to copy a rectangle region off an image with coordinates x1,y1 x2,y2 and paste it at another rectangle region of the same or another image?

Re: Copy and paste regions

Posted: 2010-09-28T10:23:58-07:00
by fmw42
convert image1 -crop WIDTHxHEIGHT+XOFFSET+YOFFSET +repage croppedimage
or
convet image1[WIDTHxHEIGHT+XOFFSET+YOFFSET] croppedimage

and then
convert image2 croppedimage -geometry +XOFFSET2+YOFFSET2 -compose over -composite result

You have to convert the x1,y1 and x2,y2 to x1=xoffset, y1=yoffset and width=x2-x1+1 and height=y2-y1+1

see
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert

Re: Copy and paste regions

Posted: 2010-09-28T10:29:35-07:00
by galv
Thank you fmw42 for your reply.
Can I perform the same operation by using the API?

Re: Copy and paste regions

Posted: 2010-09-28T11:25:33-07:00
by fmw42
I am sure you can, but I don't know any of the APIs. I only use command line and bash scripting.

Re: Copy and paste regions

Posted: 2010-09-28T18:27:29-07:00
by galv
fmw42 wrote:convert image1 -crop WIDTHxHEIGHT+XOFFSET+YOFFSET +repage croppedimage
or
convet image1[WIDTHxHEIGHT+XOFFSET+YOFFSET] croppedimage

and then
convert image2 croppedimage -geometry +XOFFSET2+YOFFSET2 -compose over -composite result

You have to convert the x1,y1 and x2,y2 to x1=xoffset, y1=yoffset and width=x2-x1+1 and height=y2-y1+1

see
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert
I don't understand what you mean by xoffset and yoffset. Could you please draw a sketch that illustrates what you mean?
Also, why add 1 to width and height?

Re: Copy and paste regions

Posted: 2010-09-28T18:47:44-07:00
by fmw42
I don't understand what you mean by xoffset and yoffset. Could you please draw a sketch that illustrates what you mean?
Also, why add 1 to width and height?
see -crop at the link above and "selecting an image region" on the page at http://www.imagemagick.org/script/comma ... essing.php

IM uses subsections specified by WIDTHxHEIGHT+XOFFSET+YOFFSET

where XOFFSET and YOFFSET are the offsets from the upper left corner of the image (at 0,0) to the upper left corner of the subsection you want and WIDTH and HEIGHT are the size of the subsection (not the lower right corner).

You need to add 1 because the difference between the first and last pixel in an image (subsection) is one off from the actual width.

Example:

last=2
first=1

diff=1, but the that does not include the starting pixel, so the real size is 2 (includes pixel 1 and pixel 2).

Re: Copy and paste regions

Posted: 2010-09-28T19:16:57-07:00
by galv
Great! Thank you, it works. It just leaves a small artifact from the crop, can that be eliminated?
I have a second question on this issue: what if I want to apply croppedimage to a bigger/smaller region in image2. How can I make croppedimage shrink/grow to that region? Maybe the question is how to set the target region in image2.

Re: Copy and paste regions

Posted: 2010-09-28T19:30:13-07:00
by anthony
You would resize the cropped image before overlaying it on the destination.

And to try and forestall the next question. You can use a mask to cut and copy a non-rectangular area.


However, if you like to explain what you want this for, we can offer even better suggestions and techniques.

Re: Copy and paste regions

Posted: 2010-09-28T19:33:39-07:00
by fmw42
galv wrote:Great! Thank you, it works. It just leaves a small artifact from the crop, can that be eliminated?
I have a second question on this issue: what if I want to apply croppedimage to a bigger/smaller region in image2. How can I make croppedimage shrink/grow to that region? Maybe the question is how to set the target region in image2.

Please clarify about the artifact or post a link to your examples.

The last use of -geometry can expand or shrink. See
http://www.imagemagick.org/Usage/layers/#composite
http://www.imagemagick.org/Usage/resize/#geometry

Other than that, you will need to use parenthesis processing with clones to resize the subsection before inserting. That is probably the safest way and most flexible.

see

http://www.imagemagick.org/Usage/basics/#parenthesis

convert image2 \( image1[WIDTHxHEIGHT+XOFFSET+YOFFSET] -resize XX% \) -geometry ... -compose over -composite result

or

convert image1 \
\( -clone 0 -crop WIDTHxHEIGHT+XOFFSET+YOFFSET +repage -resize XX% \) \
-geometry ... -compose over -composite result

See -resize and image geometry at:

http://www.imagemagick.org/script/comma ... php#resize
http://www.imagemagick.org/script/comma ... p#geometry

Re: Copy and paste regions

Posted: 2010-10-06T16:25:15-07:00
by galv
The artifact was caused by mistake in calculations.

1. Thanks Fred but is there a way to calculate the XX% automatically? I want to specify the target region's coordinates and the original region to grow/shrink accordingly to fit the space.
2. To copy multiple areas, I just string multiple -crop together and to paste multiple areas, I string multiple -geometry together. Is that correct?

Re: Copy and paste regions

Posted: 2010-10-06T17:16:38-07:00
by fmw42
galv wrote:The artifact was caused by mistake in calculations.

1. Thanks Fred but is there a way to calculate the XX% automatically? I want to specify the target region's coordinates and the original region to grow/shrink accordingly to fit the space.
2. To copy multiple areas, I just string multiple -crop together and to paste multiple areas, I string multiple -geometry together. Is that correct?

1. No way that I know for IM to compute the percent automatically. But you can script computations and store that in an argument that can be used for the geometry. Again you don't specify the target region coordinates only the upper left corner of the region (or the center if you use -gravity center, then -geometry is relative to the gravity)

variable=`convert xc: -format "%[fx:somecalculation]" info:`

see http://www.imagemagick.org/Usage/transform/#fx_escapes and http://www.imagemagick.org/script/fx.php

2). You have to have multiple -crop, -geometry, -compose and -composite

see http://www.imagemagick.org/Usage/layers/#convert

BUT as I know about it only the last -geometry will do any expansion. So you really need to use parenthesis processing to do the expansion with -resize

convert backgroundimage \
\( image2 -crop W1xH1+X1+Y1 +repage -resize XX% \) -geometry +x1+y1 -compose composemethod -composite \
\( image2 -crop W2xH2+X2+Y2 +repage -resize XX% \) -geometry +x2+y2 -compose composemethod -composite \
...
resultimage

Re: Copy and paste regions

Posted: 2010-10-06T17:35:36-07:00
by galv
1. Yes, sorry I had forgot, meant the upper left corner.
I don't understand what you mean by 'But you can script computations and store that in an argument that can be used for the geometry. ' or how fx helps the situation . Like how? Can you please demonstrate an example?

2. Thanks once again.

Re: Copy and paste regions

Posted: 2010-10-06T20:42:01-07:00
by fmw42
width=`convert logo: -format "%w" info:`
height=`convert logo: -format "%h" info:`
w2=`convert xc: -format "%[fx:$width/2]" info:`
h2=`convert xc: -format "%[fx:$height/2]" info:`

or

w2=`convert logo -format "%[fx:w/2]" info:`
h2=`convert logo -format "%[fx:h/2]" info:`

then

convert logo: -resize ${w2}x${h2} logo_half.png

Re: Copy and paste regions

Posted: 2010-10-06T20:44:35-07:00
by anthony
galv wrote:Is there a way to calculate the XX% automatically? I want to specify the target region's coordinates and the original region to grow/shrink accordingly to fit the space.
If you just specify the final size as pixels it will grow and shrink to fit that size in pixels, so do your calculations to generate the final size and loaction in terms of pixels.

Percent is a relative size change, no percent is an absolute size change.

Re: Copy and paste regions

Posted: 2010-10-07T04:01:21-07:00
by galv
Fred, if I'm not mistaken that resizes the image to half. Which is not what I want.. :/
Anthony, how? How do I specify the final size as pixels? Also, what is loaction?