How to create round corner and drop shadow by convert?

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?".
waveletboy

How to create round corner and drop shadow by convert?

Post by waveletboy »

How to create round corner and drop shadow? I tried but the speed is very slow on creating shadow. Gaussian blur cost too much time. Thanks!
http://picasaweb.google.com/waveletboy/ ... 7339123554
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create round corner and drop shadow by convert?

Post by fmw42 »

It might be nice if you gave us some information about what your command line is like?

Have you tried -shadow

http://www.imagemagick.org/Usage/convolve/#shadow
waveletboy

Re: How to create round corner and drop shadow by convert?

Post by waveletboy »

I need three steps to create such picture.

convert -size 800x600 xc:none -draw "roundRectangle 0,0 800,600 15,15" -fill white input.JPG -compose SrcIn -composite +compress inter_001.png
convert inter_001.png ( +clone -background black -shadow 80x5+8+8 ) +swap -background none -layers merge +repage +compress inter_002.png
convert -size 830x630 xc:white -draw "image Over 6,6, 0,0 'inter_002.png'" +compress final.png

Is it possible to do it with one line? Thanks.
fmw42 wrote:It might be nice if you gave us some information about what your command line is like?

Have you tried -shadow

http://www.imagemagick.org/Usage/convolve/#shadow
fockjef

Re: How to create round corner and drop shadow by convert?

Post by fockjef »

Code: Select all

convert <input file> \
-fill none -draw "matte 0,0 reset" \
-tile <input file> \
-draw "roundRectangle -1,-1 <width>,<height> 30,30" \
( +clone -background black -shadow 80x5+10+10 ) \
+swap -background none -layers merge \
+repage <output file>
waveletboy

Re: How to create round corner and drop shadow by convert?

Post by waveletboy »

You are magic! :mrgreen:
fockjef wrote:

Code: Select all

convert <input file> \
-fill none -draw "matte 0,0 reset" \
-tile <input file> \
-draw "roundRectangle -1,-1 <width>,<height> 30,30" \
( +clone -background black -shadow 80x5+10+10 ) \
+swap -background none -layers merge \
+repage <output file>
Lukas1234

Re: How to create round corner and drop shadow by convert?

Post by Lukas1234 »

I just tested this to see is it working and for some reason i getting black box.Any idea why?
pinstripe

Re: How to create round corner and drop shadow by convert?

Post by pinstripe »

I am also getting a black box. Anyone have any ideas why? I have JPEG, JPEG-2000, PNG, and TIFF support.
pinstripe

Re: How to create round corner and drop shadow by convert?

Post by pinstripe »

Apparently this only happens on JPEGs. It works fine to add shadows to PNGs, but those aren't that web-friendly. Converting the PNG back to the JPEG also results in the bad drop shadow. Any thoughts?
pinstripe

Re: How to create round corner and drop shadow by convert?

Post by pinstripe »

The solution is this:

Code: Select all

convert <file>.jpg \( +clone -background black -shadow 60x5+10+10 \) +swap -background white -layers merge +repage <output>.jpg
Notice I changed "-background none" to "-background white." Because JPEGs don't have transparency, the transparent portions get converted to black if the background is set to none. If you have "-background white," it'll flatten the transparency into the white background, producing the correct result.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to create round corner and drop shadow by convert?

Post by anthony »

The "RoundedRectangle" solution is an early solution for generating anti-aliased rounded corners on images. But as you knwo you need at least one pass first to get the image size.

A different solution generates the masking image by drawing a small corner and flip/flopping it around.

See IM Examples, Thumbnails, Rounded Corners...
http://www.imagemagick.org/Usage/thumbnails/#rounded

You can add to that command with the shadowing stuff and do BOTH image processing sequences (corners and shaodwing) in the one convert command.
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: How to create round corner and drop shadow by convert?

Post by anthony »

What OS are you using?

What version of IM are you using?

what errors do you get?

What image are you trying?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
maitrebart

Re: How to create round corner and drop shadow by convert?

Post by maitrebart »

pinstripe wrote:The solution is this:

Code: Select all

convert <file>.jpg \( +clone -background black -shadow 60x5+10+10 \) +swap -background white -layers merge +repage <output>.jpg
Notice I changed "-background none" to "-background white." Because JPEGs don't have transparency, the transparent portions get converted to black if the background is set to none. If you have "-background white," it'll flatten the transparency into the white background, producing the correct result.
Would anyone know how to do the same but with a background image instead of having a white background?

I tried:

Code: Select all

convert <file>.jpg \( +clone -background black -shadow 60x5+10+10 \) +swap -texture mytexture.gif -layers merge +repage <output>.jpg
but it wouldn't work: the whole background is black. In fact I tried different combinations of -background and -texture within the command line without any success.

If possible, I would prefer the image (and its shadow) not to begin at the top left corner but be offset by few pixels (e.g. +5+5).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create round corner and drop shadow by convert?

Post by fmw42 »

try this using the IM internal images netscape: and logo:

infile1="netscape:"
infile2="logo:"
ww=`convert $infile1 -ping -format "%w" info:`
hh=`convert $infile1 -ping -format "%h" info:`
wwm1=`convert xc: -format "%[fx:$ww-1]" info:`
hhm1=`convert xc: -format "%[fx:$hh-1]" info:`
convert $infile1 \
\( +clone -threshold "100%" -fill white -draw "roundrectangle 1,1 $wwm1,$hhm1 25,25" \) \
-alpha off -compose CopyOpacity -composite \
-background none -compose Over -gravity center -extent ${ww}x${hh} \
\( +clone -background black -shadow 60x4+4+4 \) +swap \
-background none -flatten \
$infile2 +swap -gravity center -composite netscape_rounded_shadow_logo.png

first line after the 6 variables are defined is input image infile1=netscape
second line makes a black image from the netscape: and draws a white roundrectangle
third line puts the white and black roundrectange as the alpha channel of the netscape image
fourth line adds a black shadow
fifth line flattens (combines) the shadow with the rounded netscape
sixth line overlays the shadowed rounded netscape over the infile2=logo: image
maitrebart

Re: How to create round corner and drop shadow by convert?

Post by maitrebart »

fmw42 wrote:try this using the IM internal images netscape: and logo: [...]
Thanks fmw42. Your script looks to work. However, when I replace the infile variables by actual files, I get the following errors:

Code: Select all

convert: unable to open image `../bkgd_photo.jpg': No such file or directory.
convert: no such image `p_cotegaspesie5s.jpg'.
convert: missing an image filename `netscape_rounded_shadow_logo.png'.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to create round corner and drop shadow by convert?

Post by Bonzo »

Post your code.

I pressume you are not adding : to your image name ? This is only for built in Image Magick images.
Post Reply