Apply clipping path and create drop shadow on trimmed 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
tobiasg
Posts: 2
Joined: 2017-09-07T07:39:23-07:00
Authentication code: 1151

Apply clipping path and create drop shadow on trimmed image

Post by tobiasg »

Hi there,
I need to convert .tif files containing a clipping path to .png while applying some additional conversions:
  1. apply the clipping path and make the background (everything outside the clipping path) transparent
  2. crop/trim the image so its width and height equal the remaining content (everything that's inside the clipping path)
  3. resize to a certain fixed width/height without distorting the image
  4. create a soft drop shadow to the content inside the clipping path
  5. add a transparent border of 10% of the resulting width/height
  6. convert to .png
Also I need to find a way to decrease the file size. My current solution creates .png files of 6 or more MB, even if the width is just about 1000x1000 px.

I found a way to do all this (except the reduction of the file size) on the command line by concatenating two "convert" commands. However, in my specific case IM is used as a conversion tool by a separate application, and I need to find a way to execute the steps described above in one single command line.
My concatenated command looks like this:

Code: Select all

convert source.tif -clip -alpha set -background transparent -trim -delete 1-5 -resize 850x1136 miff:- | convert - \( +clone -background black -shadow 20x10+0+50 \) +swap -background none -layers merge -border 10% target.png
Here is the link to a sample .tif file: https://www.dropbox.com/s/gdoacxpnuc3l0kv/salad.tif
And here is the link to a sample .png file, as I would like it to be generated: https://www.dropbox.com/s/9ywwo8zgoczkvh9/salad.png

Any ideas will be greatly appreciated.
Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply clipping path and create drop shadow on trimmed image

Post by fmw42 »

Try this. It works for me on IM 6.9.9.12 Q16 Mac OSX. Unix syntax. The resulting size is about 619 KB. That smaller size may be due to the -strip command just before the resize.

Code: Select all

 convert -quiet salad.tif[0] -alpha transparent -clip -alpha opaque +clip -strip -resize 850x1136 \
 \( +clone -background black -shadow 20x10+0+50 \) +swap -background none -layers merge +repage \
 -bordercolor none -border 10% salad_fred.png
Image

Note that this does not match your result. Probably because the shadow adds width and height to the resized image and then you add a 10% border after the shadow is added.

Alternately, you could add the border before the shadow:

Code: Select all

convert -quiet salad.tif[0] -alpha transparent -clip -alpha opaque +clip -strip \
 -resize 850x1136 -bordercolor none -border 10%  \
 \( +clone -background black -shadow 20x10+0+50 \) \
 +swap -background none -layers merge +repage \
 salad_fred2.png
Image



Or if you want a specific output size, then you could just use -extent.

Code: Select all

 convert -quiet salad.tif[0] -alpha transparent -clip -alpha opaque +clip -strip -resize 850x1136 \
 \( +clone -background black -shadow 20x10+0+50 \) +swap -background none -layers merge +repage \
 -gravity center -background none -extent 1068x1033 salad_fred3.png
Image
tobiasg
Posts: 2
Joined: 2017-09-07T07:39:23-07:00
Authentication code: 1151

Re: Apply clipping path and create drop shadow on trimmed image

Post by tobiasg »

Thanks fmw42 for your input.
I checked out your commands and apparently they don't produce the same outcome on my environments (local: Mac OSX, remote: CentOS). Both environments have IM 7.0.5 versions installed. Since you are using a 6.x version I installed 6.9.9-11 as well as 7.0.7-0 on my Mac and alas it's working. I still need to up- or downgrade IM on the server, but I'm quite optimistic that it'll be working there, too.
I altered your first example slightly by putting in a "-trim" before the "-strip", which effectively trimmed the unnecessary transparent parts on each side of the clipped image.

One additional question: is it relevant whether I add the "[0]" to the name of the source file? What difference does this make compared to using just the file name?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Apply clipping path and create drop shadow on trimmed image

Post by GeeMack »

tobiasg wrote: 2017-09-08T05:29:01-07:00One additional question: is it relevant whether I add the "[0]" to the name of the source file? What difference does this make compared to using just the file name?
Some image formats such as TIF and GIF can contain more than one layer. When reading an image into IM, you can specify which layer (or layers) to use by putting the layer number in square brackets attached to the end of the file name. The first layer is 0. In your original command you did "-delete 1-5" to get rid of the unwanted layers. By using something like "filename.tif[0]" you won't even bring those extra layers into the command at all.

Other possible notations might include "filename.tif[1,3]" to use only the second and fourth layer, or maybe "filename.tif[2-4]" to use the third, fourth, and fifth layers. You can even read in only the last layer by using something like "filename.tif[-1]".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply clipping path and create drop shadow on trimmed image

Post by fmw42 »

This command works fine on IM 6.0.6.0 Q16 Mac OSX.

Code: Select all

magick -quiet salad.tif[0] -alpha transparent -clip -alpha opaque +clip -strip -resize 850x1136 \
 \( +clone -background black -shadow 20x10+0+50 \) +swap -background none -layers merge +repage \
 -bordercolor none -border 10% salad_fred.png
I suggest you upgrade your IM versions, especially IM 7, since it is new and bugs and enhancements are made often. Perhaps a bug in your IM 7 version has been fixed.
Post Reply