Search code examples
androidimagemagickgraphicsmagick

Process 9-Patch Image with ImageMagick


How do I do something with a 9-Patch image with ImageMagick?

To simplify, I need to apply any ImageMagick operation on a rectangle that starts at upper-left at (1, 1) and ends at lower-right at (width - 2, height - 2). The operation must not touch the 1 pixel border of the image.

For example, I want to replace the colors in the rectangle described above. How do I do that?


Solution

  • There are several ways of doing this. It kind of depends what you want to do in the middle bit :-)

    One way, is to clone the image, and shave off a 1 pixel border all the way round, do your operations on the "slightly smaller" image and then composite the result back into the original.

    convert -gravity center image.png                   \
      \( +clone -shave 1x1 -fill blue -colorize 100% \) \
      -composite result.png
    

    So, I start with a red rectangle, copy and shave, fill with blue and then paste.

    enter image description here