Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded corners using ImageMagick (bakground transparent or white)

I'm trying to add rounded corners to my images using ImageMagick.

If the input image is a PNG or GIF file my script is working correctly.

But if my input file is a JPEG file, the corners are black. I'd like to use a custom corner color in that case (ex. white) any idea ?

Here's my working bash script :

convert -size "$W"x"$H" xc:none -draw "roundrectangle 0,0,$W,$H,$R,$R" $MASK
convert $SRC -matte $MASK -compose DstIn -composite $DST

Parameters are :

$SRC : the input image $W : width of input image $H : height of input image $MASK : the mask image which contains transparent corners $DST : the resulting image with rounded corners.

Thanks in advance.

like image 357
PM. Avatar asked Dec 16 '09 16:12

PM.


People also ask

What CSS styles if any can you use to display the image with gently rounded corners?

The CSS property border-radius adds rounded corners on images. You can round all of the image's corners or just select corners, vary the radius on different corners or display an image in the shape of an oval or a circle.


1 Answers

Finally found a solution :

convert -size "$W"x"$H" xc:none -draw "roundrectangle 0,0,$W,$H,$R,$R" $MASK
convert $SRC -matte $MASK -compose DstIn -composite $TMP_PNG

I'm using a "temp" PNG file as destination. If the output format is not GIF or PNG, I use the "flatten" function of ImageMagick with the white color as background.

convert $TMP_PNG -background white -flatten $DST

For PNG output : simply copy $TMP_PNG to $DST

For GIF output : simply convert $TMP_PNG to $DST

Else : flatten the image as said before.

Hope that helps.

like image 69
PM. Avatar answered Nov 18 '22 07:11

PM.