Perspective distort and losing part of the 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Perspective distort and losing part of the image

Post by Bonzo »

As I read it with the perspective distort ( and other distort methods ) if part of the image is outside the original it is lost.

I am trying to make a "cube" and I can get the 2 sides with shear OK but I am using perspective distort for the top and it is not working.
Is there a way around this ? I wonder if I should shear it first then distort it ?

Code: Select all

<?php
// Distort the images to use
exec("convert src.jpg -background none -shear 0x20 shear_y1.png ");
exec("convert src.jpg -background none -shear 0x160 shear_y2.png ");
exec("convert -background none -gravity Center shear_y1.png shear_y2.png +append output_shear.png"); 
exec("convert src.jpg -matte -background none -virtual-pixel background -distort Perspective '0,0 0,239, 319,239, 319,0, 0,0 300,100, 600,0, 300,-100' -trim car4.png");
// Join the first 2 images along the longest edge
exec("convert -background none -gravity Center shear_y1.png shear_y2.png +append output_shear.png"); 
// Composite the top image
exec("convert -size 700x700 xc:none output_shear.png -gravity center -composite car4.png -geometry -140-115 -composite -trim output_shear1.png");
?>
Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Perspective distort and losing part of the image

Post by anthony »

Use +distort, and the output image will be adjusted to contain the whole of the source image (if posible) with an appropriate virtual canvas offset.
That later part allows you to determine exactly the actual position of the output image, without lossing any image data. You can use things like '-layers merge' to merge images with different offsets together correctly, again without lossing any actual image data.

An example of this is planned, but I have been side tracked by other lower level improvements so have not created it yet.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Perspective distort and losing part of the image

Post by Bonzo »

That worked thanks Anthony.

Image
Post Reply