Search code examples
imagemagickimagemagick-convert

How to do four point distort with Imagemagick


I want to place a logo on a image. I have 4 (x,y) coordinates. The logo should be placed between this area only with some skew effect as given below.

enter image description here


Solution

  • It is always best to provide clean separate input imagery (in a addition to your result) and your ImageMagick version and platform.

    Nevertheless, I cropped out the two images as:

    enter image description here

    enter image description here

    Then I measured the corner coordinates of the G image clockwise from the top left corner.

    Then I measured the colored circle coordinates in similar order (green, red, pink, blue).

    Then I did the +distort perspective and composited the two images. The sequence of coordinates is: inx1,iny1 outx1,outy1 .... inx4,iny4 outx4,outy4, where the G image is the in and the bag image is the out.

    Here is the command in unix ImageMagick 6 syntax. For Windows remove the \s. For ImageMagick 7, use "magick" rather than "convert"

    convert bag.png \( G.png -virtual-pixel none +distort perspective "0,0 75,280  116,0 155,311  116,119 141,367  0,119 64,329" \) -layers flatten +repage result.png
    

    enter image description here

    Reference: http://www.imagemagick.org/Usage/distorts/#perspective

    EDIT:

    If you make the white background of the G image transparent, then it will look better.

    convert bag.png \( G.png -fuzz 15% -transparent white -virtual-pixel none +distort perspective "0,0 75,280  116,0 155,311  116,119 141,367  0,119 64,329" \) -layers flatten +repage result2.png
    

    enter image description here