Thanks for both example images
My ideea would be to create the shadow in a different layer, after that modify the geometry (tilt, perspective) of the shadow and after that merge the 2 layers. Easy to say, hard to do
Your idea is spot on. but with one small twist.
The shape is standing on the ground on the bottom most row of pixels. rather than trying to calculate and do all your calculations basied on this position 'flip' the image so that the ground 'row' is at the top of pixel '0'.
This simplifies the working, as you only need to do the appropriate 'shear' at origin or '0,0'. Though the intermedite working images will be upside down...
Here is my final solution...
Code: Select all
convert sampng.png -flip \
\( +clone -background Black -shadow 60x10+0+0 \
+distort Affine '0,0 0,0 100,0 100,0 0,100 -100,50' \) \
+swap -background white -layers merge \
-flip -fuzz 2% -trim +repage \
output.png
Each line is a processing step as follows...
- flip image so ground is at 0,0
- create 'blurred shadow' preserves 'layer offsets
- shear distort the shadow, along the Y=0 line.
The numbers define the final angles and so not relate in any way to the size or shape of the image, only to the final 'shearing' effect wanted.
In many ways this is just like a simple -shear (see Warping Images, Shear, but using +distort so that it preserves and uses 'layer offsets, as part of the warping process.
NOTE: the size of the distorted image is larger than expected, due to the 'cut off corners. It also generates large negative offsets, so be careful saving and viewing the resulting layer image.
- place shadow layer behind the shape and merge to form a new layer, just big enough to hold all images. Also add a white background. Note layer images will have negative offsets!
- Final clean up. flip image back, trim the excess image generated from distorting a rectangular image, and junk the layer offsets.
And you have a nice affine warped shadow.
You can adjust the shadow any way you like simply by adjusting the
movement of the
0,100 control point, by changing the last two numbers only. I move it left
-100 pixels, and compress it vertically to
50 to get a 'compress shear'.
Rather than flipping, a variation is to just set the ground line of the image using
Code: Select all
sampng.png -flip +distort SRT '0,0 1,-1 0'
this flips the image, then 'layers' flips the image so that Y=0
along the bottom row. The final '-flip' is now not needed, and images remain right way up.
Code: Select all
convert sampng.png -flip +distort SRT '0,0 1,-1 0' \
\( +clone -background Black -shadow 60x10+0+0 \
+distort Affine '0,0 0,0 100,0 100,0 0,100 -100,50' \) \
+swap -background white -layers merge \
-fuzz 2% -trim +repage \
output.png
Note you may have to change the direction of the shear to get the shadow as you want.
Addendum.... I have now added this to IM Examples,
3d Shadows, using Affine Shears which should appear in a day or so.
Also the example is taken further with an additional shadow processing to provide variable blurring so that the shadow becomes more blurry the further it gets from the base line of the shape.