Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Smooth White Border Around Circular Image

I'm using pgmagick to generate a circular thumbnail. I'm using a process similar to the one discussed here, which does indeed produce 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 composite the thumbnail over that, letting the white circle "peak out" from under the thumbnail and create a border effect. Here's the pgmagick code I used to achieve that:

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 fairly distorted with choppy edges -- not production ready. If I take drawer.stroke_antialias(False) out, it's even worse.

Any ideas on making this border smoother using pgmagick?

like image 272
user1427661 Avatar asked Apr 03 '14 18:04

user1427661


People also ask

How to create a white border around cut-outs in Canva?

To achieve a plain white border, use the following settings: You can play around here and create borders in completely different styles. And voila, that’s it. This is how you can add this nice border around your cut-outs. Like watching a quick video tutorial? Here are the steps to create a white image border in Canva:

How do I add a border to my Canva design?

This method has the benefit that you can create a border in any weight, transparency and color you wish. Not just plain white. Place your image in a new Canva design. We are now using the background remover feature available in Canva Pro to create a cut out photo. Click on your image, go to Effects and then select the Background Remover tool.

How do I add a border to a picture in Photoshop?

Upload the image, then drag and drop the picture onto the layout. Click the picture, then click the Edit Image. Under Shadows, choose Glow. To add a solid border to the photo, adjust the Blur to 0 and the Transparency to 100.

How do you make a round corner on a picture?

Free online tool to make round corner image in a simple steps. Drop image in tool, set the corner radius in slider, then click Round corner button to process the image. Once process completed, preview of round corner image is displayed along with download button. What is radius in tool?


2 Answers

I leave it as a simple exercise for the reader to convert this solution from commandline to pgmagick (see more below). The code underlying pgmagick is the same as that used by the commandline.

You could draw the circle larger and then "resize" it down. This ameliorates the jaggy look of the circle by averaging the edge with the surrounding background during the resizing operation.

Instead of

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

Do this:

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

You could try other sizes and decide which is the smallest that satisfies you, e.g.,

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

This commandline works on 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 not clear that pgmagick offers "resize". The documentation shows "img.scale" which will probably result in a jaggy circle. Using "-scale" on the commandline examples above instead of "-resize" does indeed produce the same jaggy image.

pgmagick does however allow you to specify the filter type, as in

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

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

like image 83
Glenn Randers-Pehrson Avatar answered Sep 30 '22 13:09

Glenn Randers-Pehrson


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)
like image 43
dlemstra Avatar answered Sep 30 '22 15:09

dlemstra