6.6.0-0 2010-03-05 Q16 - Mac OSX 10.6.8. and this is what I'm getting:
note that ball.jpg in no longer centered in this output.
Clone centering & shadowing over image background
- 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
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
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
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Clone centering & shadowing over image background
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.
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: 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!
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
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]'
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/
https://imagemagick.org/Usage/
Re: Clone centering & shadowing over image background
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!
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!
- 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
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.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Clone centering & shadowing over image 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.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)
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 .... \)
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/
https://imagemagick.org/Usage/
Re: Clone centering & shadowing over image background
Yes, I just noted the 2x sigma calculation. will try those approaches. thanks!