Page 1 of 1

How to Keystone an image (pinch the bottom retain the top))

Posted: 2016-01-23T16:14:37-07:00
by hoover
I've exhausted hours of playing with coordinates to try to get an image to keystone (distort perspective on bottom left and bottom right by an equal distance with no impact to the top anchor points). I'm doing this via command line. (as suggested in other threads, Wave Displacement didn't give decent results... perspective seems to retain the proportions best)

Image dimensions = 258Wx505H
I want to pinch both sides on the bottom by ~35px. Ideally, I want to pinch by a % of the original image, so I can pinch the bottom by 10% on each side - equally.

The code below works for a 258x505 image, however, I can't dynamically replicate this for images of various sizes.

Code: Select all

convert magick.png -virtual-pixel transparent \
          -distort Perspective '0,0,0,0  0,%h,35,%h  %w,0,%w,0  160,50,160,65' \magick2.png
My primary questions:
1 - how do I change the bottom left coordinate to 10% vs a fixed pixel coord of 35 (as seen above)? I tried to dozens of % configurations and they all throw errors. In order to change images of various sizes on the fly, I need a dynamic value, or model to define ~10%.
2 - bottom right coordinates of 160,50,160,65... for the life of me, nothing other than 65 works. I can change the other numbers, but nothing yields a simple pinch left effect by ~10%.

note: I've tried wave displacement to bulge the image, but it causes the image to become totally distorted. Distort perspective seems to give me the results that I need, but finding a simple coordinate model seems impossible. I can generate simple % based coordinates based on the original image size, but everything seems totally unpredictable.

Re: How to Keystone an image (pinch the bottom retain the top))

Posted: 2016-01-23T17:12:57-07:00
by snibgo
In the last corner, you are changing the y-coordinate. From your description, I think you want to change the x-coordinate.

If there are (w) pixels on each row, the last pixel on each row is (w-1).

For expressions, you need the "%[fx:...]" syntax. For example, Windows BAT syntax:

Code: Select all

set sPERSP=^
0,0,0,0,^
0,%%[fx:h-1],35,%%[fx:h-1],^
%%[fx:w-1],0,%%[fx:w-1],0,^
%%[fx:w-1],%%[fx:h-1],%%[fx:w-36],%%[fx:h-1]

%IM%convert ^
  wizard: ^
  -virtual-pixel transparent ^
  -distort perspective "%sPERSP%" ^
  w2.png
I put the long transformation into an environment so I can split it into lines, for human readability.

You can use complex expressions such as:

Code: Select all

%%[fx:w-0.36*w]
%%[fx:w*0.64]
%%[fx:w+sqrt(h)]

Re: How to Keystone an image (pinch the bottom retain the top))

Posted: 2016-01-23T20:16:11-07:00
by hoover
snibgo, thank you so much for a quick reply. the suggestion for %'s was helpful. I played around a bit more and found the -distort arc function. Don't know how I missed that earlier, but it gives me a nice keystone effect without distorting & bulging the image too much.

For reference, here's the command line code that I'm using to distort images based in a percentage

Code: Select all

convert magick.png -virtual-pixel black -distort Arc 10%   magick2.png