Clipping mask and adding feather

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
orjan
Posts: 6
Joined: 2018-05-29T04:18:12-07:00
Authentication code: 1152

Clipping mask and adding feather

Post by orjan »

I have photos with clipping mask in photoshop and want to remove/replace the background using ImageMagick.
All my photos have white background (black in reference photo to highlight my problem).
When I remove the background using IM today, the edge gets very pixelated compared to the result I want.
And compared to what I get from photoshop.
See reference here:
https://www.dropbox.com/s/5fw6l9u4i6w5v ... k.png?dl=0

Could someone please help me with a good code that helps me get a feather on the edge or at least lighten up the sharp edge that Imagemagick creates?

All my photos are at least 2000x2000px (300ppi)
Will that effect the code?

Orjan
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clipping mask and adding feather

Post by snibgo »

orjan wrote:When I remove the background using IM today, ...
Without seeing your command, we can't suggest how to improve it.

If you link to the original input with a clip path, perhaps we can advise. However, I can't download files larger than about 2 MB.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clipping mask and adding feather

Post by fmw42 »

It would help if you provide your original image. Also your version of ImageMagick and your platform.
orjan
Posts: 6
Joined: 2018-05-29T04:18:12-07:00
Authentication code: 1152

Re: Clipping mask and adding feather

Post by orjan »

Thanks guys.
The original file I sent to IM you`ll find here:
https://www.dropbox.com/sh/681tf50se7nj ... uBgha?dl=0

The file I sent to IM is located in the "upload" folder. My current result after IM command is located in the "download-after" folder.

The command I am using today is:
convert input.jpg -alpha transparent -clip -alpha opaque +clip +repage output.png
(Replacing a clipping-mask with transparent background (jpg to png))

Just for record, this is done using the inRiver PIM platform, that only support a single line command for imagemagick transformations.

Will the dimensions of the photo effect the command in terms of the quality of the feather on the mask?
And will it be possible to contract the original mask with 2px using a command in IM?
The reason I am asking is that the clipping mask is often located 1 or 2 pixels outside the object I want to remove the background from...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clipping mask and adding feather

Post by fmw42 »

The latest version of ImageMagick has improved anti-aliasing. The result I get is almost as good as from your other tool. Nevertheless, you can antialias with blurring and you can shrink the edges by any number of pixels using -morphology erode. Try the following commands. Unix syntax.

Code: Select all

convert input.jpg -alpha transparent -clip -alpha opaque +clip +repage output1.png

Code: Select all

identify -quiet -format "%[8BIM:1999,2998:#1]" input.jpg  |\
convert -quiet input.jpg \( - -negate \) \
-alpha off -compose copy_opacity -composite \
out2.png

Code: Select all

identify -quiet -format "%[8BIM:1999,2998:#1]" input.jpg  |\
convert -quiet input.jpg \( - -negate -blur 0x1 -level 50x100% \) \
-alpha off -compose copy_opacity -composite \
out3.png

Code: Select all

identify -quiet -format "%[8BIM:1999,2998:#1]" input.jpg  |\
convert -quiet input.jpg \( - -negate -morphology erode diamond:1 -blur 0x1 -level 50x100% \) \
-alpha off -compose copy_opacity -composite \
out4.png
orjan
Posts: 6
Joined: 2018-05-29T04:18:12-07:00
Authentication code: 1152

Re: Clipping mask and adding feather

Post by orjan »

Hi! I have tried the code suggested here. The feather works really well, but the problem I am facing now, is that the clipping mask does not remove the background. Any suggestions?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clipping mask and adding feather

Post by fmw42 »

Your file worked for me. Do you have another image that does not work? What is your IM version and platform?
orjan
Posts: 6
Joined: 2018-05-29T04:18:12-07:00
Authentication code: 1152

Re: Clipping mask and adding feather

Post by orjan »

Thanks for checking fmw42
The engine uses the convert.exe from ImageMagick
Platform: Windows
Version: Unknown (Should be the newest version)

Do you have any idea why it is not working for me?
orjan
Posts: 6
Joined: 2018-05-29T04:18:12-07:00
Authentication code: 1152

Re: Clipping mask and adding feather

Post by orjan »

I tried installing ImageMagick on my local Windows PC, and used the following code:

magick identify -quiet -format "%[8BIM:1999,2998:#1]" "C:\ImageMagick\Product-image-with-clipping-mask.jpg" |^
magick convert -quiet "C:\ImageMagick\Product-image-with-clipping-mask.jpg" ( - -negate -morphology erode diamond:1 -blur 0x1 -level 50x100% ) ^
-alpha off -compose copy_opacity -composite ^
"C:\ImageMagick\Product-image-with-clipping-mask.png"

The result was an all transparent image.
Platform: Windows
Versions: ImageMagick-7.0.7-38-Q16-x64-dll.exe

Upload Image: https://www.dropbox.com/s/rllrp890vpkyf ... k.jpg?dl=0
Result: https://www.dropbox.com/s/myvqucmf6ff76 ... k.png?dl=0
orjan
Posts: 6
Joined: 2018-05-29T04:18:12-07:00
Authentication code: 1152

Re: Clipping mask and adding feather

Post by orjan »

Btw, will the naming conversion of the actual clipping mask on the photo have any effect on how the code should be specified? If example the clipping path is called "Clipping path" or "Path1"?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clipping mask and adding feather

Post by fmw42 »

It seems to need an -alpha off added in IM 7. Try

(unix syntax)

magick identify -quiet -format "%[8BIM:1999,2998:#1]" image.jpg |\
magick -quiet image.jpg \( - -alpha off -negate -morphology erode diamond:1 -blur 0x1 -level 50x100% \) \
-alpha off -compose copy_opacity -composite \
result.png

(windows syntax)

magick identify -quiet -format "%[8BIM:1999,2998:#1]" image.jpg |^
magick -quiet image.jpg ( - -alpha off -negate -morphology erode diamond:1 -blur 0x1 -level 50x100% ) ^
-alpha off -compose copy_opacity -composite ^
result.png
piratexses
Posts: 3
Joined: 2018-06-19T02:08:05-07:00
Authentication code: 1152

Re: Clipping mask and adding feather

Post by piratexses »

Hi,

I’m trying to implement the functionality in C#, but have some problems doing so. If anyone might be able to help me out, it will be very much appreciated.
Post: viewtopic.php?f=27&t=34174
Post Reply