Compose regions with original image

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
Krismu

Compose regions with original image

Post by Krismu »

Hi,

I'm trying to add a simple swirl on a PNG image with alpha.
I need to apply it to a sub-region of my image.
Unfortunately, the result is not what expected as the region is badly composed with "Over" method on my initial image.
I would want the image to be distorted and I got the distortion over the image.

Here is the command line I use:

Code: Select all

convert image1.png -region 75x75 -swirl 325 image2.png
(image1 is 100x100).

So my question is:
Is there a way to define how regions are composed with original image ?

Thanks,
K.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Compose regions with original image

Post by anthony »

You are missing an offset for the region to place it in the appropriate position.

For example this shows the wizard with a rather curvy wand

Code: Select all

convert logo: -region  150x150+200+80 -swirl -120 show:
See IM examples, Regions for more (though not very much) information.
http://www.imagemagick.org/Usage/warping/#region
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose regions with original image

Post by fmw42 »

I have not used -region before and would be curious to know if directly compatible with -distort?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Compose regions with original image

Post by anthony »

I have not tried, but would be surprised if it didn't work with -distort.
Regions are handled at a higher level than the other operators.

Yeap they work. But with some problems.

But user specified control points are relative to an image that contains a layer offset. as such
control points may not be working as you expect and could be different from distort to distort.

For example

Code: Select all

convert logo: -region 150x150+200+80 -distort SRT 10 show: 
shows the reagion is being rotated but around the original center of the image (due to image offsets). BUT

Code: Select all

convert logo: -region 150x150+200+80 -distort SRT '75,75, 1,10' show: 
rotates the image with a center at 75,75 pixels relative to the top left coordinates of the image!

Also when regions are pasted back into the original image, they do not appear to use layer offsets, but are always positioned according to the original region offset. (composite geometry, not layer page offset)


Unless someone cares to take the time to work out exactly HOW regions should work and be applied, then I think regions will not be very useful with distorts.

I also thing I'll need to look at a refinement of how coordinates are to be handled in SRT when a layer offset is present. I think the center should always default to the center of the actual image and not the center of the virtual canvas. On the other hand user coordinates probably should be relative to image for -distort, and relative to the virtual canvas for +distort.

Comments welcome.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Krismu

Re: Compose regions with original image

Post by Krismu »

Thanks Anthony.

However, it seems that the offsets are not mandatory.
I've added them:

Code: Select all

convert image1.png -region 75x75+20+20 -swirl 325 image2.png
And the result is the same, swirled image is pasted over the original, so I can see the original through the distorted region.

According to the documentation:
the "-region" operator extracts an area of image to perform operations in, and will only restore that area (using Composite Over) when either the end of the command is reached, or another "-region" is requested
The "composite over" is what I would want to change.
I tried to add "-compose" everywhere in the command line but with no success.
Last edited by Krismu on 2009-05-20T12:56:40-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: Compose regions with original image

Post by fmw42 »

just a thought, but is your image1.png partially transparent. if so that may explain why -compose over is showing the underneath image. and if so, can you remove the transparency?
Krismu

Re: Compose regions with original image

Post by Krismu »

Yes my image contains transparency, but alpha channel is variable, not only 0 or 255.
It would do if I replace the transparency by a solid color, do my stuff, and put back some transparency.
But with anti-aliasing, I would have some intermediate colors everywhere, and I would'nt be able to get back a clear image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose regions with original image

Post by fmw42 »

You probably then need to replace -region with a composite operation using -geometry

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

Then you have control over the -compose setting
Krismu

Re: Compose regions with original image

Post by Krismu »

OK, I don't use regions anymore.
Instead, I delete a sub-part of my image, do my stuff on a cropped-part of it, and compose both images together ...

Code: Select all

convert image1.png ( -size 75x75 xc:red ) -geometry +0+0 -compose dst-out -composite ( image1.png -crop 75x75+0+0 -swirl 300 ) -compose over -composite image2.png
This way I delete the upper corner (75*75) of my image, do my swirl on the cropped part (75*75 again), and compose them.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Compose regions with original image

Post by anthony »

Krismu wrote:OK, I don't use regions anymore.
Instead, I delete a sub-part of my image, do my stuff on a cropped-part of it, and compose both images together ...

Code: Select all

convert image1.png ( -size 75x75 xc:red ) -geometry +0+0 -compose dst-out -composite ( image1.png -crop 75x75+0+0 -swirl 300 ) -compose over -composite image2.png
This way I delete the upper corner (75*75) of my image, do my swirl on the cropped part (75*75 again), and compose them.
You don't need 'clear' the region of the original image using a 'dst-out'. but can replace it back into the original using "-compose copy" which replaces transparency as well. If you was a irregular region you can compose the image back using a a third greyscale masking image.

Code: Select all

convert image1.png \( +clone -crop 75x75+0+0 +repage -swirl 300 \) \
             -compose copy -geometry +0+0 -composite image2.png
That should save you one heavy image processing step.

See Compose Copy
http://www.imagemagick.org/Usage/compose/#copy


There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results! -- Anthony Thyssen
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Krismu

Re: Compose regions with original image

Post by Krismu »

Thanks, right this one works even better.
Post Reply