Search code examples
imagemagicknode-imagemagick

How can I stretch images while preserving corners using ImageMagick?


I want to stretch images, while preserving 4 corners intact. Similar stuff in iOS is setting capInsets, for UIImage.


Solution

  • Imagine a hash symbol (tic-tac-toe grid) on your image, with the corner squares being the part you don't want to distort.

    1. Divide your image into 3 subimages at the vertical lines with the -crop option.
    2. Stretch (or compress) the middle image horizontally.
    3. Reassemble the 3 parts into one image (+append)
    4. Divide the image into 3 subimages at the horizontal lines with -crop.
    5. Stretch the middle piece vertically.
    6. Reassemble (-append).