Transparent, rounded corners

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
makrell66
Posts: 7
Joined: 2007-02-06T00:55:36-07:00

Transparent, rounded corners

Post by makrell66 »

I've been looking around, but I'm not able to find out how to create transparent, rounded corners.
The tricky bit is the transparency. I manage to create rounded corners with a colored background, but can not find out how to proceed to make them transparent.

- m66 -
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Transparent, rounded corners

Post by Bonzo »

To make the colour in an image transparent you can do this.

Code: Select all

exec("/usr/local/bin/convert start_image.jpg -transparent red output_image.gif");
makrell66
Posts: 7
Joined: 2007-02-06T00:55:36-07:00

Re: Transparent, rounded corners

Post by makrell66 »

Thanks, that helped me a lot.

One other thing; all examples I have seen to create rounded corners, four individual corner images are created, and then composed on to the original image.

Will it be possible to create one mask (a rectangular image) with all four corners (colored), and use this to compose on to the original image?

- m66 -
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Transparent, rounded corners

Post by Bonzo »

You could do that; I tried it out before and the result is at the bottom of this page: http://www.rubblewebs.co.uk/imagemagick/other.php

Code: Select all

exec("/usr/local/bin/convert snow.jpg -composite masktemp2.png -composite temp2.png");
exec("/usr/local/bin/convert temp2.png -transparent PeachPuff round_snow2.gif");
unlink( 'temp2.png' );
It worked but you have to make sure all the original photo gets covered by the mask and the colour you are using to make transparent is not in the original photo you want to keep.

This discussion came up before and you can use "DstOut" but I can not find the method I used for this. If I do I will post the code.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Transparent, rounded corners

Post by magick »

Try -vignette. Is that similar to what you want? If so you need to draw a mask (vignette draws an ellipse) and use -flatten to combine the layers.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Transparent, rounded corners

Post by Bonzo »

This is some code for DstIn and DstOut; the transparency is showing up black - I got over this before but can not remember how I did it.
Hopefuly Anthony will read this post and put me straight :D

http://www.rubblewebs.co.uk/imagemagick ... rell66.php

Code: Select all

exec("/usr/local/bin/convert -size 358x358 xc:none -fill black -draw \"circle 177,177 177,10\" mask.png");
exec("/usr/local/bin/convert earth.jpg -gravity center -compose DstOut mask.png -composite result_dst_out.png");
exec("/usr/local/bin/convert earth.jpg -gravity center -compose  DstIn mask.png -composite result_dst_in.png");
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Transparent, rounded corners

Post by el_supremo »

Try this to see if it's the sort of thing you want:

Code: Select all

convert -size 640x480 xc:none -fill white -draw 'roundRectangle 15,15 624,464 15,15' logo: -compose SrcIn -composite logo_rounded_frame.png
Pete
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Transparent, rounded corners

Post by anthony »

All interesting.. here is my 2 cents

IM Examples, Thumbnails, Adding Fluff
http://www.imagemagick.org/Usage/thumbn ... ded_border
Also look at the example above it using a unusual 'virtual-pixel' bluring technique.

I did have a corner example like what you describe, but it was removed as it was really more of a repeat of the method used in 'fancy' borders further down. I plan to return it to IM examples but in another form.

kepp the examples comming. preferably with actual results, or using built-in images as the test image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
bram12
Posts: 14
Joined: 2008-10-14T02:40:20-07:00

Re: Transparent, rounded corners

Post by bram12 »

hi anthony

I tried your example on http://www.freeonlinephotoeditor.com (as many examples :) )
But the round corner does not seem to work, see the site and then 'effects' and then 'round corners'. The background is not tranparant.

PHP code:

Code: Select all

$size_img= getimagesize($filenameclean);
$width=$size_img[0]+2;
$height=$size_img[1]+2;
exec('convert '.extractname($_REQUEST["imageFileName"]).' -border 2 -matte -channel RGBA -threshold -1 -background none -fill none -stroke black -strokewidth 3 -draw \'roundrectangle 1,1 '.$width.','.$height.' 15,15\' '.$imagesurse_corner_overlay.'');
exec('convert '.extractname($_REQUEST["imageFileName"]).' -border 2 -matte -channel RGBA -threshold -1 -background none -fill white -stroke black -strokewidth 1 -draw \'roundrectangle 1,1 '.$width.','.$height.' 15,15\' '.$imagesurse_corner_mask.'');
exec('convert '.extractname($_REQUEST["imageFileName"]).' -matte -bordercolor none -border 2  '.$imagesurse_corner_mask.' -compose DstIn -composite '.$imagesurse_corner_overlay.' -compose Over -composite '.$imagesurse.'');
Any idea??
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Transparent, rounded corners

Post by Bonzo »

Looking at the details of the image created it is a jpg file and jpg dos not support transparency.
bram12
Posts: 14
Joined: 2008-10-14T02:40:20-07:00

Re: Transparent, rounded corners

Post by bram12 »

pffff... yes sorry you are correct.
Jorge
Posts: 3
Joined: 2011-09-01T06:21:19-07:00
Authentication code: 8675308

Re: Transparent, rounded corners

Post by Jorge »

i found the link with the code provided in here unverse.net/css-rounded, just if anyone needs it by now. :)
Last edited by Jorge on 2012-06-21T01:00:51-07:00, edited 2 times in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Transparent, rounded corners

Post by anthony »

I would say reselecting a thread from 3 years ago is generally not a good idea.

The pointed out artical is a basic technique, but more about generating images for web page markup, than adding rounded corners to actual images which is the topic of this (old) discussion.

The only thing I would have to say is...
  1. when drawing the circle, draw it on a even numbers sized image
  2. with even numbered images the center of the circle is drawn at a 0.5 offset. EG: (size-1)/2 as draw uses pixel coordinates
  3. now divide the image by using equal area crop
    http://www.imagemagick.org/Usage/crop/#crop_equal
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply