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

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
hoover
Posts: 2
Joined: 2016-01-23T15:44:17-07:00
Authentication code: 1151

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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)]
snibgo's IM pages: im.snibgo.com
hoover
Posts: 2
Joined: 2016-01-23T15:44:17-07:00
Authentication code: 1151

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

Post 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
Post Reply