Search code examples
imagemagickimagemagick-convertperspectivedistortion

Strange effect using 'distort perspective' on multiple images using ImageMagick


I want to create a 3D effect on an image so it looks like a folder, using the +distort Perspective' commands from ImageMagick.

However, if I use the distort multiple times (on different layers) in ImageMagick the result doesn't look correct.

Please see the attached image, left is correct, but the top-right corner of the right image looks strange.

left is correct but right is incorrect

I have used the following commands:

convert square.jpg -brightness-contrast 0x0 -resize 700x700 \
            \( -clone 0 -flip -crop 700x140+0-0 -size 700x140 gradient:gray60-black -compose CopyOpacity -composite \) \
            -append front.png

convert square.jpg -brightness-contrast -50x0 -resize 700x700 \
\( -clone 0 -flip -crop 700x140+0-0 -size 700x140 gradient:gray60-black -compose CopyOpacity -composite \) \
-append inner.png

convert -background None -virtual-pixel transparent -background white  \
            \( inner.png +distort Perspective '0,0 0,0  700,0 735,-35  700,700 735,665  0,700 0,700' \) \
            \( front.png +distort Perspective '0,0 0,0  700,0 665,35  700,700 665,735  0,700 0,700' \) \
            -layers merge +repage 3dimage.jpg

I'm using ImageMagick v6.8

I'm not sure if I understand the layers in ImageMagick correctly. Can someone see what's wrong? Thanks!


Solution

  • I'm not sure if I understand the layers in ImageMagick correctly. Can someone see what's wrong?

    You're doing fine. Just need to repeat the -virtual-pixel transparent on the sub-image for front.png (and perhaps the inner.png too).

    convert -background None -virtual-pixel transparent -background white  \
            \( inner.png +distort Perspective '0,0 0,0  700,0 735,-35  700,700 735,665  0,700 0,700' \) \
            \( front.png -virtual-pixel transparent +distort Perspective '0,0 0,0  700,0 665,35  700,700 665,735  0,700 0,700' \) \
            -layers merge +repage 3dimage.jpg
    

    3dimage.jpg