Page 1 of 1

Rounded corners for jpg image

Posted: 2012-06-28T16:25:07-07:00
by hknight
I want to to round the corners like this:
Image

This works if the image type is a png or gif image:

Code: Select all

convert rose: -alpha set -virtual-pixel transparent -channel A -blur 0x8  -threshold 50% +channel rounded_corner.gif
However the corners are not rounded if I use a jpg file type:

Code: Select all

convert rose: -alpha set -virtual-pixel transparent -channel A -blur 0x8  -threshold 50% +channel rounded_corner.jpg
I realize that jpg images do not support a transparency. How can I make the rounded corners red?

Re: Rounded corners for jpg image

Posted: 2012-06-28T17:46:22-07:00
by anthony
Draw the corner, and overlay..

See IM Examples...
http://www.imagemagick.org/Usage/thumbnails/#rounded

The thrid example draws red corners (or any color you like), specifically for the JPEG image problem.

Code: Select all

  convert thumbnail.gif \
    \( +clone -crop 16x16+0+0  -fill white -colorize 100% \
       -draw 'fill black circle 15,15 15,0' \
       -background Red  -alpha shape \
       \( +clone -flip \) \( +clone -flop \) \( +clone -flip \) \
     \) -flatten  rounded_corners_red.png
Image

It relies on -flip and -flop not only transforming the image, but also the image position on a virtual canvas, which was preserved by the -crop from the original image.

Re: Rounded corners for jpg image

Posted: 2012-06-28T17:55:31-07:00
by anthony
Another way, is to add alpha, do the transparent corner, then use the new (relativity new) operator -alpha remove to remove the transparency to background color correctly

See IM Examples, Masking and Backgrounds, Removing Transparency from Images.
http://www.imagemagick.org/Usage/masking/#remove

I have now added a link to this area from the Thumbnail, Rounded Corners example.

Re: Rounded corners for jpg image

Posted: 2012-06-28T18:11:19-07:00
by hknight
Thanks, Anthony!

I use this code to make sure that an image is exactly 150px by 80px and it is sized to avoid as much blank canvas space as possible:

Code: Select all

convert -size 150x80 xc:red null: (  tree.png -resize "150x80^>" ) -gravity Center -layers Composite 150x80.jpg
How can I combine that with the rounded-corner code so the image with rounded corners is exactly the specified dimensions?

Re: Rounded corners for jpg image

Posted: 2012-06-28T18:26:54-07:00
by anthony
hknight wrote:Thanks, Anthony!

I use this code to make sure that an image is exactly 150px by 80px and it is sized to avoid as much blank canvas space as possible:

Code: Select all

convert -size 150x80 xc:red null: (  tree.png -resize "150x80^>" ) -gravity Center -layers Composite 150x80.jpg
How can I combine that with the rounded-corner code so the image with rounded corners is exactly the specified dimensions?
Either do the rounded corners to the image in parenthesis (before overlay) or do it after (before the save)

However rather than use centered compose, you can get the same thing more simply using -extent. See IM Examples, Thumbnails, Pad Out the Thumbnail
http://www.imagemagick.org/Usage/thumbnails/#pad

Re: Rounded corners for jpg image

Posted: 2012-06-28T18:42:34-07:00
by hknight
This gives an error:

Code: Select all

convert (-size 150x80 xc:red null: (  rose: -resize "150x80^>" ) -gravity Center -layers Composite) ( +clone -crop 16x16+0+0  -fill white -colorize 100% -draw "fill black circle 15,15 15,0" -background Red  -alpha shape ( +clone -flip ) ( +clone -flop ) ( +clone -flip ) ) -flatten  rounded_corners_red.png
I will try the extent thing after the error is fixed but I only want to change one thing at a time. Thanks!