Rounded corners for jpg image

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
hknight
Posts: 32
Joined: 2007-08-02T10:16:15-07:00

Rounded corners for jpg image

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Rounded corners for jpg image

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Rounded corners for jpg image

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
hknight
Posts: 32
Joined: 2007-08-02T10:16:15-07:00

Re: Rounded corners for jpg image

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Rounded corners for jpg image

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
hknight
Posts: 32
Joined: 2007-08-02T10:16:15-07:00

Re: Rounded corners for jpg image

Post 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!
Post Reply