ImageMagick: distort perspective according to a vector path

I want to use ImageMagick to transform a rectangular image (first page of a PDF, created using convert in.pdf[0] cover.png) to the following white shape. I have the vector file and know about distorting in ImageMagick but have now clue which numbers to pass to the distort command. Can I somehow convert my path to the perspective parameters?

ImageMagick should be used to be able to automate the process easily.

enter image description here


Yes. Take for example following images,

enter image description here enter image description here

Images 1-2: Foreground and background

Then using following imagemagick shell script*:

convert                         ^
  bg.png                        ^
 -page +100+100                 ^
    (                           ^
      fg.png                    ^
     -matte                     ^
     -virtual-pixel transparent ^
     -distort Perspective       ^
     "0,0,20,105 206,0,157.5,60 0,264,130,303 206,264,302,235" ^
    )                           ^
 -layers flatten                ^

you get:

enter image description here

* this is using Windows if you use a Mac or a Linux change ^ with \ note there can be no space after the end of line symbol.

How was this done?

How did i get the numbers. First you need 2 squares one original and one distorted. Measure the points of upper corner upper corner distorted and tack them together for a 4 number group (0,0,20,105). This is the first pair. Then measure the top right corner and the distorted top right corner (in same coordinate system). Do the same for lower left corner and lower right corner.

enter image description here

Image 3: Points of original and distorted one. Yellow line depicts extents of background image.

Source : Link , Question Author : Alex , Answer Author : joojaa

Leave a Comment