Clone centering & shadowing over image background

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?".
motumbo
Posts: 16
Joined: 2011-07-26T12:56:12-07:00
Authentication code: 8675308

Re: Clone centering & shadowing over image background

Post by motumbo »

6.6.0-0 2010-03-05 Q16 - Mac OSX 10.6.8. and this is what I'm getting:

Image

note that ball.jpg in no longer centered in this output.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clone centering & shadowing over image background

Post by fmw42 »

Yes, I had not noticed - the combined ball and shadow together are centered so that neither are centered.

So this works better to do what you want. Separate the composites into two steps. Offset the shadow with -geometry and composite over sky, then composite the ball centered over the previous result.


convert sky.png ball.jpg \
\( -clone 1 -background black -shadow 100x10+10+10 \) \
\( -clone 0 -clone 2 -gravity center -geometry +100+100 -compose over -composite \) \
-delete 0,2 +swap -gravity center +geometry -compose over -composite sky_ball4.jpg
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Clone centering & shadowing over image background

Post by anthony »

Using offsets in layered images is not hard. you just need to thing relative, not absolute!

Shadow understands and uses virtual offsets. So if the ball has the right offset, the shadow will be correct relative to the ball. That is how that operator was designed! So if you are using the right offsets, at least the right offset relative to each other, then the images will 'layer' correctly, and the generated shadow will be offset correctly too!

For relative centered offsets, easiest way is to set a negative offset, so that the canvas origin (0,0) is at the center of the image of both images. The images are thus both positioned centered relative to each other. Strange but it works.

Code: Select all

convert sky.png ball.jpg -alpha set -set page '-%[fx:w/2]-%[fx:h/2]' \
        \( +clone -background black -shadow 80x12+12+12 \) +swap \
        -layers merge  +repage   sky_ball.jpg
The relative centered offset is done by the first line in the following example. the offset is only removed at the end after they have been merged together

If you don't like using a -negative offset, then you could add a fixed positive offset to the %[fx:...] expressions,
or even add the offset of the background sky image! Eg:

Code: Select all

 -set page '+%[fx:(u.w-w)/2]+%[fx:(u.h-h)/2]'
That would make the center offset -0-0 for the sky image but the right offset for the ball image. and they are right for the final result, so only a simplier flatten layering operation is needed (no +repage either!)

See IM Examples..
http://www.imagemagick.org/Usage/basics/#page
http://www.imagemagick.org/Usage/layers/#merge
http://www.imagemagick.org/Usage/blur/#shadow

In future a special -layer method to set offsets relative to background (first image) may be provided, but really it is just a simple calculation!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
motumbo
Posts: 16
Joined: 2011-07-26T12:56:12-07:00
Authentication code: 8675308

Re: Clone centering & shadowing over image background

Post by motumbo »

Perfect, makes sense!

btw not sure if this is the appropriate channel but the -shadow itself introduces some artifacts when the sigma value is high, mostly notable with light backgrounds (high contrast between the shadow and the background)

note that the borders of the shadow are not smooth as they should; seems like the internal blur applied is a little higher then the actual sigma mask.

convert sky.png ball.jpg \
\( -clone 1 -background black -shadow 100x40+10+10 \) \
\( -clone 0 -clone 2 -gravity center -geometry +100+100 -compose over -composite \) \
-delete 0,2 +swap -gravity center +geometry -compose over -composite sky_ball.jpg

Thanks for all the help!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clone centering & shadowing over image background

Post by fmw42 »

motumbo wrote:Perfect, makes sense!

btw not sure if this is the appropriate channel but the -shadow itself introduces some artifacts when the sigma value is high, mostly notable with light backgrounds (high contrast between the shadow and the background)

note that the borders of the shadow are not smooth as they should; seems like the internal blur applied is a little higher then the actual sigma mask.

convert sky.png ball.jpg \
\( -clone 1 -background black -shadow 100x40+10+10 \) \
\( -clone 0 -clone 2 -gravity center -geometry +100+100 -compose over -composite \) \
-delete 0,2 +swap -gravity center +geometry -compose over -composite sky_ball.jpg

Thanks for all the help!

I believe that may be because the blur used is gaussian and never gets quite to zero within the distance internally computed for cutoff. Gaussian blurs only get to zero at infinite distance.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Clone centering & shadowing over image background

Post by anthony »

motumbo wrote:btw not sure if this is the appropriate channel but the -shadow itself introduces some artifacts when the sigma value is high, mostly notable with light backgrounds (high contrast between the shadow and the background)
the reason for this is that the amount of added extra space for the blur is set to only 2x the sigma. really it should be three.
For small blurs 2x is fine whcih was why I used in in my original request for a -shadow operator so long ago. But for larger sigmas you can start to see some artifacts as you report.

See the shadow equivalence example...
http://www.ict.griffith.edu.au/anthony/ ... _internals

One solution is to add your own extra border (transparent) around the original image before generating the shadow.

A better variation is to add that extra border to the clone image and then add a relative negative offset adjustment to keep it aligned.
For example..

Code: Select all

   \( +clone -alpha set -bordercolor none -border 10 -repage -10-10! \
      -shadow .... \)
yes it is a pain but it will do the job.

Another solution is to fix the shadow operator itself to add a 3x border internally, of course that does then mean ALL shadow generated images will actually be much larger for all usage.

I would however prefer to keep the amount of border that is added to be fixed. If I didn't mind a variable border addition I could make the border dependant on the internally calculated radius used for -blur 0x{sigma} operations, though it tends to be far larger than is typically visible and also becomes variable relative to the IM's compile time Quality setting.

Hmm one improvement. Blur and thus shadow is actually a linear operation, but when used on sRGB images (whcih is a non-linear colorspace), it will appear much darker than it actually should.

To correct this add -channel A -gamma 2.2 +channel immediately after the -shadow operation. Better still, wrapper the layer composition with -colorspace sRGB -layers merge -colorspace RGB

Either method will greatly reduce the darknesss of the semi-transparent areas of the shadow and make it behave more correctly in terms of Human Color Perception
http://www.imagemagick.org/Usage/color_ ... perception
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
motumbo
Posts: 16
Joined: 2011-07-26T12:56:12-07:00
Authentication code: 8675308

Re: Clone centering & shadowing over image background

Post by motumbo »

Yes, I just noted the 2x sigma calculation. will try those approaches. thanks!
Post Reply