Copy-paste an image region

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
henrypijames

Copy-paste an image region

Post by henrypijames »

What's the easiest way to copy a rectangle region of an image onto another place in the same image, for example, copy the region 20x20 at +10+10 relatively +100+100?

So far I've come up with:

Code: Select all

convert old.png ( +clone -crop 20x20+10+10 ) -geometry +110+110 -composite new.png
It works, but since "-geometry" repages the image, I have to specify the absolute position +110+110. For greater clarity, is there a way to use +100+100 instead? Or a way that's completely different and more efficient?

And what if instead of copy-paste, I want to cut-paste (say, leaving the original region transparent)? What's the easies way then?
Last edited by henrypijames on 2009-05-28T14:26:48-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Copy-paste an image region

Post by fmw42 »

henrypijames wrote:What's the easiest way to copy a rectangle region of an image onto another place in the same image, for example, copy the region 20x20 at +10+10 relatively +100+100?

So far I've come up with:

Code: Select all

convert old.png ( +clone -crop 20x20+10+10 ) -geometry +110+110 -composite new.png
It works, but since "geometry" repages the image, I have to specify the absolute position +110+110. For better clarity, is there a way to use +100+100 instead? Or a way that's completely different and more efficient?
That is about as efficient as it gets. But you may want to modify it to add +repage for safety in removing the virtual canvas.

Code: Select all

convert old.png ( +clone -crop 20x20+10+10 +repage ) -geometry +110+110 -composite new.png
It is pretty much the same thing to use -page ... -flatten rather than -geometry ... -composite
see http://www.imagemagick.org/Usage/layers/#flatten

You can also draw one image over another. see http://www.imagemagick.org/Usage/layers/#draw

You will have to experiment and time test to see which is most efficient.

Perhaps Anthony can suggest something else.
henrypijames wrote:And what if instead of copy-paste, I want to cut-paste (say, leaving the original region transparent)? What's the easies way then?
You can use -draw to fill in a transparent region. See

http://www.imagemagick.org/Usage/draw/#matte
henrypijames

Re: Copy-paste an image region

Post by henrypijames »

Yeah, I've just figured out that "-page ... -flatten" is better because this way I can chain multiple copy-paste:

Code: Select all

convert old.png -page +100+100 ( -clone 0 -crop 20x20+110+110 ) -page -100-100 ( -clone 0 -crop 20x20+370+370 ) -flatten new.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Copy-paste an image region

Post by fmw42 »

henrypijames wrote:Yeah, I've just figured out that "-page ... -flatten" is better because this way I can chain multiple copy-paste:

Now, is there a way to "-draw" the last image from the existing sequence onto the first? Because that might be an interesting solution. Also, does "-draw" respect the virtual canvas of the overlaying image?
Not sure what you mean here?
henrypijames wrote:Regarding cut-paste, you're basically saying there's no simpler way to do it other than copy-paste then redraw the original region?
No, I am saying you can simply use -draw to fill any rectangular region of an image to remove all colors, i.e. make it transparent. see my example at http://www.fmwconcepts.com/imagemagick/ ... colorblock
henrypijames

Re: Copy-paste an image region

Post by henrypijames »

fmw42 wrote:Not sure what you mean here?
I mean: Can "-draw" only overlay an image from a file, or can it overlay an image from the existing sequence? In other words, after "convert file1.png ( file2.png -negate )", can I somehow "-draw" the negated file2 over file1?
fmw42 wrote:No, I am saying you can simply use -draw to fill any rectangular region of an image to remove all colors, i.e. make it transparent.
Well that's pretty obvious, but it doesn't answer my question if there's a simpler way to cut-paste than to copy-paste and delete - whether the deletion is done with "-draw" or something else doesn't matter.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Copy-paste an image region

Post by fmw42 »

henrypijames wrote:
fmw42 wrote:Not sure what you mean here?
I mean: Can "-draw" only overlay an image from a file, or can it overlay an image from the existing sequence? In other words, after "convert file1.png ( file2.png -negate )", can I somehow "-draw" the negated file2 over file1?
Why bother when you can just as easily use composite or flatten? But here is one way:

I have insert the 70x46 sized rose: image that has been negated at 100,100 in the logo: image
convert logo: \( rose: -negate -write mpr:rose +delete \) \
-draw "image over 100,100 0,0 'mpr:rose'" \
logo_rose.jpg
Image

henrypijames wrote:
fmw42 wrote:No, I am saying you can simply use -draw to fill any rectangular region of an image to remove all colors, i.e. make it transparent.
Well that's pretty obvious, but it doesn't answer my question if there's a simpler way to cut-paste than to copy-paste and delete - whether the deletion is done with "-draw" or something else doesn't matter.
You did not look at my example at http://www.fmwconcepts.com/imagemagick/ ... colorblock it draws a transparent rectangle into an image, i.e. it clears the image to transparency within the rectangle. No composites needed. Do I misunderstand what you want?

Here is the logo image with a 70x46 area at 100, 100 made transparent:

convert logo: \
-draw "fill red rectangle 100,100 170,146 fill none matte 135,123 floodfill" \
logo_trans.gif
Image

But you have to pick some color, such as the red that I used, as an intermediate color AND it must not be the same color as what you have just outside that area. Otherwise the matte fill will spread into those areas. Fortunately, my red was not exactly the same as the red color in the logo image. It is probably safer to use composite with a mask image and compose copy_opacity.
henrypijames

Re: Copy-paste an image region

Post by henrypijames »

fmw42 wrote:You did not look at my example at http://www.fmwconcepts.com/imagemagick/ ... colorblock it draws a transparent rectangle into an image, i.e. it clears the image to transparency within the rectangle. No composites needed. Do I misunderstand what you want?
I'm pretty sure you did. I'm not interested in how to clear a region of the image - that's pretty straight forward. I'm interested in cut-paste, i. e. copying and deleting at the same time. It's like when you move a file to another directory, you move it directly instead of copying it first and deleting the original then (of course it's what the OS internally does if source and destination directories belong to different volumes, but the user doesn't need to do a two-step).

The "mpr:" thing is very useful, thanks. I'll go some more testing.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Copy-paste an image region

Post by fmw42 »

With regard to the transparency, I thought I was replying to your question:
And what if instead of copy-paste, I want to cut-paste (say, leaving the original region transparent)? What's the easies way then?
henrypijames

Re: Copy-paste an image region

Post by henrypijames »

fmw42 wrote:With regard to the transparency, I thought I was replying to your question:
And what if instead of copy-paste, I want to cut-paste (say, leaving the original region transparent)? What's the easies way then?
I was talking about "leaving the original region transparent" not as a stand-alone operation, but as one of two (simultaneous) effects of cut-paste - the other, rather obvious one being that the region in question being copied somewhere else. I'm looking for something that can cause those two effects in a combined manner - or to repeat myself, a more efficient way to cut-paste than copy-paste and delete original.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Copy-paste an image region

Post by anthony »

Yeah, I've just figured out that "-page ... -flatten" is better because
this way I can chain multiple copy-paste:

Code: Select all

convert old.png -page +100+100 ( -clone 0 -crop 20x20+110+110 ) -page
-100-100 ( -clone 0 -crop 20x20+370+370 ) -flatten new.png
I think you missed something...

-page is used to set the virtual canvas offset of NEW
images being read in or created. It does NOT modify existing images. to
modify an existing image use -repage

Going back to the original problem, this is the equivalent of using
-geometry +110+110

Code: Select all

convert old.png  \( -clone 0 -crop 20x20+10+10  -repage +110+110 \)
-flatten new.png
Note however that -geometry is
-gravity effected a page offset is NOT.

But what a page offset can to is move the copy you cropped out, 'relative' to
its original position.

Code: Select all

convert old.png  \( -clone 0 -crop 20x20+10+10  -repage +100+100\! \)
-flatten new.png
This should again produce the same result moving the copy of the rectangle 100
pixels down and right.

NOTE: while -flatten and
-composite can produce similar results. You should
know that -flatten in this case is much slower. That
is because internally it does a canvas creation AND 2 separate composite
operations - one for every image it is given. That is because it is designed
to handle mergea whole list of images together, not just two images.

This is typically not a problem, as people usually don't notice the speed
difference.

The -mosaic and -layers merge
methods are exactly like -flatten, but with different
'canvas generation' effects, that effect the final size and clipping of
overlay, in the results.


If speed or memory is an issue (REALLY BIG IMAGES) Other wise ignore this, as
it is more complicated! You can try...

Another alternate method is -layers composite, but
it will require a special 'null:' image between the destination and source
images, as it is designed to merge two lists of images.

Code: Select all

convert old.png  null: \( -clone 0 -crop 20x20+10+10  -repage +100+100\!
\)  -layers composite new.png
This only does ONE compose, and will apply BOTH geometry AND page offsets
simultaneously!
So is actually just as fast as a normal -composite method.

ASIDE: Geometry is used as the global multi-image offset setting (with
gravity) for the two image lists, while the page offset is saved into and used
for individual image offsets.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Copy-paste an image region

Post by anthony »

henrypijames wrote:I was talking about "leaving the original region transparent" not as a stand-alone operation, but as one of two (simultaneous) effects of cut-paste - the other, rather obvious one being that the region in question being copied somewhere else. I'm looking for something that can cause those two effects in a combined manner - or to repeat myself, a more efficient way to cut-paste than copy-paste and delete original.
To any computer 'cutting' is always a separate operation. Their is no such thing as 'moving' data, only 'copying' data, or 'modifying data' .

copy-paste is making a copy, and a modification.
cut-paste is really a copy-paste-cut.

as such you will have to make the original area 'transparent' yourself in come way.

In images making an area transparent is very different to overlaying an image, so you will need to to clear the area you need to do some extra work.

here is the original copy and paste...

Code: Select all

convert old.png ( +clone -crop 20x20+10+10 ) -geometry +110+110 -composite new.png
Here is my cut and paste

Code: Select all

convert old.png  -alpha set   \( +clone -crop 20x20+10+10 +repage \) \
             \( -clone 0--1 -geometry +110+110 -composite \)  -swap 0 +delete \
             \( +clone -alpha opaque \) +swap +delete \
             -geometry +10+10 -compose DstOut -composite   new.png
This does the copy paste, but keeps a copy of the image that was 'cropped'
It then makes that image full opaque and uses it to clear the original position.
The -alpha set ensures the original image does have an alpha channel to begin with.

However as it is a rectangular area a more direct -draw is probably faster. though other than
Fred's (fwm42) solution, draw does not provide you with a simple method of 'erasing' an object
like a rectange from an image (though it should be able to as this is done in SVG all the time).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
henrypijames

Re: Copy-paste an image region

Post by henrypijames »

anthony wrote:

Code: Select all

convert old.png  \( -clone 0 -crop 20x20+10+10  -repage +100+100\! \)
-flatten new.png
This should again produce the same result moving the copy of the rectangle 100
pixels down and right.
Yes, it does - "!" does the trick, thanks a lot!
anthony wrote:NOTE: while -flatten and
-composite can produce similar results. You should
know that -flatten in this case is much slower. That
is because internally it does a canvas creation AND 2 separate composite
operations - one for every image it is given. That is because it is designed
to handle mergea whole list of images together, not just two images.
The composition how-to says:
In Alpha Composition only two images are ever involved, the 'source' and the 'destination' (or 'background'), though a third Masking Image may also be provided to limit the area of effect of the composition.
So I thought "-composite" can handle only two layers at a time - obviously that's not the case. However, the order in which "-composite" put the images in is very strage:

Code: Select all

convert old.png 1.png 2.png -composite new.png
It seems "-composite" puts 2.png in the middle and 1.png on the top.

Regardless of this problem, "-composite" still doesn't work here as it apparently repositions the cropped image back to +0+0 - inconsistent with what it does with the command in #1. Why is that?
anthony wrote:To any computer 'cutting' is always a separate operation. Their is no such thing as 'moving' data, only 'copying' data, or 'modifying data' .

copy-paste is making a copy, and a modification.
cut-paste is really a copy-paste-cut.
First, it's not "always" like that. As I've noted, when you move a file - which can be done in cut-paste fashion in a graphical OS like Windows - to a directory on the same disk volume, the OS doesn't do copy-delete, but rewrite the directory entry without touching the file itself.

Second, I know the with image manipulation, it's very unlikely that an arbitrary region can be directly "moved" in that way. But as I wrote before, I was hoping maybe IM has a "shortcut" that lets the user do easy cut-paste while it does the two-step internally.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Copy-paste an image region

Post by anthony »

henrypijames wrote:The composition how-to says:
In Alpha Composition only two images are ever involved, the 'source' and the 'destination' (or 'background'), though a third Masking Image may also be provided to limit the area of effect of the composition.
So I thought "-composite" can handle only two layers at a time - obviously that's not the case.
That is correct!!! composite and convert -compose only every merge two image at a time, EVER.

Flatten is a higher level operator designed to use multiple calls to composite to merge multiple images together, starting with a 'background' image of the current -background color, that is the same size as the 'virtual canvas' of the first image given.

However, the order in which "-composite" put the images in is very strage:

Code: Select all

convert old.png 1.png 2.png -composite new.png
The order is swapped as it makes the operator work better in a larger sense.
so it is background or destination image first, then the overlay or source image.

This way you can slowly build up a large image by overlaying new images being generated or read in, and modified before being overlaid.

The "composite" command, which only does ONE major task, does in in the order you would use when 'talking' about the compose operation.... source OVER destination.
Regardless of this problem, "-composite" still doesn't work here as it apparently repositions the cropped image back to +0+0 - inconsistent with what it does with the command in #1. Why is that?
Not ceratin what you mean, but I'll make a guess...

Their are two completely separate methods of handling offsets.
Compose uses Geometry with Gravity.
While Flatten / Mosaic / Merge is designed to handling 'layered images' which position images using 'virtual offsets' (no gravity) relative to a larger (usually) virtual, or imaginary canvas.

see http://www.imagemagick.org/Usage/compose/#geometry
anthony wrote:To any computer 'cutting' is always a separate operation. Their is no such thing as 'moving' data, only 'copying' data, or 'modifying data' .

copy-paste is making a copy, and a modification.
cut-paste is really a copy-paste-cut.
First, it's not "always" like that. As I've noted, when you move a file - which can be done in cut-paste fashion in a graphical OS like Windows - to a directory on the same disk volume, the OS doesn't do copy-delete, but rewrite the directory entry without touching the file itself.

But that is not a cut/copy/paste operation at all. It is a link operation.
The file itself does not 'move' only the very small 'link' to the file in the directory structure.

Of course the index itself is copyed, and then deleted. In fact you can have as many linked to the same file from multiple places in a directory hierarchy. These are called 'hardlinks'.

So in fact it is still three operations, copy, paste and delete. But of the file, but of the LINK to teh file.

If you were to move a file from one disk drive (or different file system) to another, then the file itself really would have to be copied (lots more time), and the old one really deleted (can be quick, depending on the file system type).

A computer has no idea of 'move' as a single operation, only 'copy' and 'delete'.

However we are getting off topic.

Second, I know the with image manipulation, it's very unlikely that an arbitrary region can be directly "moved" in that way. But as I wrote before, I was hoping maybe IM has a "shortcut" that lets the user do easy cut-paste while it does the two-step internally.
[/quote]
Sorry no. It can copy the image into another 'layer' image, then delete the old one. But it is not 'built in as such.


However creating a better way of doing the 'delete' to a transparency, should be developed.

The problem here is that transparency is special, in that you can NOT 'draw' transparency in the usual way, you need to 'erase' it using a masking composition of some kind. This gets even harder
when semi-transparent edge pixels become involved, which is why I needed to insure the mask was correctly filled out into the complete 'cutting rectangle' in my example (more work :-( )
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply