Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw quality image stroke (border) with Imagemagick or PIL?

I tried to draw image stroke (border) to my png image. originalph_res
(*expected 19px stroke layer style result by Photoshop)

and I found helpful script from imagemagick forum. (imagemagick discourse)

convert source.png -background '#2a7940' -alpha background -channel A -blur 9x11 stroked.png

but my best trial is far from Photoshop result. im_res

I tried many combinations of Radius*Sigma but it seems the best result from blur trick.. (-blur 9x11 for this one)

Question
* Can I have better image stroke from Imagemagick or PIL or other CLI tools?
* If so, how..?
Thank you so much for reading this question.

like image 905
sngjuk Avatar asked Mar 04 '23 22:03

sngjuk


1 Answers

When you need clean edges on an effect you create with ImageMagick, you can double the size of your input image, run the operations, then resize it back to its original input size. It takes longer to run the operations, but the result can be substantially improved. Here's an example command...

convert bomb-source.png -write mpr:in -resize 200% \
   -channel A -morphology dilate disk:38 +channel \
   -fill green -colorize 100 -resize 50% mpr:in -composite result.png

That command starts by reading the input image and storing a copy in a memory register named "mpr:in".

Then it resizes the input to 200% and uses "-morphology" to dilate the shape by about 38 pixels. That will work out to about 19 pixels after the image is reduced back to its input size.

Next it colorizes that new shape to make it green and resizes it 50%, which is back to the original size.

The command finishes by bringing back that "mpr:in" copy of the original image and compositing it over the modified green piece.

Increasing the size, working on that, and decreasing it after modifying it is called "super sampling". It's a common technique that results in smoother edges, but it comes at the cost of speed.

Edited to add the output image... enter image description here

like image 104
GeeMack Avatar answered Mar 16 '23 22:03

GeeMack