Create a smooth white border around a circular image

I use pgmagick to create a circular thumbnail. I use a process similar to the one described here , which really creates a nice circular thumbnail for me. However, I need a white border around the radius of the circle.

My initial approach was to create a new image of a slightly larger white circle with a transparent background and sketch over it, letting the white circles pop out from under the thumbnail and create a border effect. Here is the pgmagick code I used for this:

border_background = Image(Geometry(220, 220), Color('transparent')) drawer = Draw() drawer.circle(110, 110, 33.75, 33.75) drawer.fill_color(Color('white')) drawer.stroke_antialias(False) border_background.draw(drawer.drawer) border_background.composite(original_thumbnail, 0, 0, CompositeOperator.OverCompositeOp) 

This β€œworks,” but the surrounding white border is rather distorted by variable edges, not ready for production. If I take drawer.stroke_antialias (False), this is even worse.

Any ideas on how to make this border smoother using pgmagick?

+6
source share
2 answers

I leave this as a simple exercise so that the reader can convert this solution from commandline to pgmagick (see below). The code underlying pgmagick is the same as the command line.

You can draw a circle larger and then "resize". This improves the distorted appearance of the circle by averaging the edge with the surrounding background during the resize operation.

Instead

 gm convert -size 220x220 xc:none -fill white \ -draw "circle 110,110, 33.75,33.75" \ original.png 

Do it:

 gm convert -size 880x880 xc:none -fill white \ -draw "circle 440,440, 135,135" \ -resize 25% resized.png 

You can try other sizes and decide which one is the smallest, for example,

 gm convert -size 440x440 xc:none -fill white \ -draw "circle 220,220, 67.5,65.5" \ -resize 50% resized.png 

This command line works with both GraphicsMagick ("gm convert") and ImageMagick ("convert")

Looking at the pgmagick documentation at http://pgmagick.readthedocs.org/en/latest/cookbook.html#scaling-a-image it is unclear what pgmagick suggests "resize". The documentation displays "img.scale", which is likely to result in a jagged circle. Using "-scale" in the above command line examples instead of "-resize" really creates the same jaggy image.

However, pgmagick allows you to specify the type of filter, as in

  img.scale((150, 100), 'lanczos') 

which should be equivalent to "-resize" and this is what you want.

+7
source

You will get a better result if you choose a different approach:

 # First draw the thumbnail inside the circle. background = Image(Geometry(220, 220), Color('transparent')) drawer = Draw() drawer.circle(110, 110, 33.75, 33.75) drawer.fill_color(Color('white')) background.draw(drawer.drawer) background.composite(original_thumbnail, 0, 0, CompositeOperator.InCompositeOp) # Draw only the border of the circle on top of the thumbnail inside the circle border = Image(Geometry(220, 220), Color('transparent')) drawer.fill_color(Color('transparent')) drawer.stroke_color(Color('white')) drawer.stroke_width(3) border.draw(drawer.drawer) background.composite(border, 0, 0, CompositeOperator.OverCompositeOp) 
+5
source

All Articles