Page 1 of 1

Clipping mask and adding feather

Posted: 2018-05-29T06:37:45-07:00
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

Re: Clipping mask and adding feather

Posted: 2018-05-29T07:21:45-07:00
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.

Re: Clipping mask and adding feather

Posted: 2018-05-29T09:06:49-07:00
by fmw42
It would help if you provide your original image. Also your version of ImageMagick and your platform.

Re: Clipping mask and adding feather

Posted: 2018-05-30T02:07:03-07:00
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...

Re: Clipping mask and adding feather

Posted: 2018-05-30T09:53:10-07:00
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

Re: Clipping mask and adding feather

Posted: 2018-06-07T07:24:14-07:00
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?

Re: Clipping mask and adding feather

Posted: 2018-06-07T09:22:00-07:00
by fmw42
Your file worked for me. Do you have another image that does not work? What is your IM version and platform?

Re: Clipping mask and adding feather

Posted: 2018-06-11T01:36:56-07:00
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?

Re: Clipping mask and adding feather

Posted: 2018-06-11T01:52:35-07:00
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

Re: Clipping mask and adding feather

Posted: 2018-06-11T01:55:04-07:00
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"?

Re: Clipping mask and adding feather

Posted: 2018-06-11T10:32:10-07:00
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

Re: Clipping mask and adding feather

Posted: 2018-06-19T02:32:28-07:00
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