Gap between clip-path and image content

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
jd.wiese
Posts: 28
Joined: 2012-11-21T00:14:48-07:00
Authentication code: 6789

Gap between clip-path and image content

Post by jd.wiese »

Hi!

We are about to switch from ImageMagick 6.9 to 7.0 in our application and so I have done some testing and found a problem in the clipping operation. Usually we take a TIF image with a path and convert it into a PNG with the background removed using the following command:

convert image.tif -alpha transparent -clip -alpha opaque -strip image.png

And here is the problem:
With IM 6 the image was clipped "closer". The edge between the image and the transparency was barely visible. With IM 7 this edge is much more noticeable. For example when I set the PNG image on a black background there are white pixels all around the image.

Here are two examples:

http://www.awg88.de/imagemagick/2016-05-12/image_v6.png
http://www.awg88.de/imagemagick/2016-05-12/image_v7.png

The first one is made with IM ImageMagick-6.9.3-Q16 and the second one with ImageMagick-7.0.1-Q16. You can see the difference when you switch between the two images. There are much more white pixels around the image in the second image.

Here are some questions:
- any suggestions on how to get the same/better results as in IM 6?
- is it possible to adjust the distance between the clip-path and the image?
- is it possible to create the alpha-channel with antialiasing around the edges?

The original TIF image can be downloaded here:

http://www.awg88.de/imagemagick/2016-05-12/image.tif

Any help would be appreciated!

JD
jd.wiese
Posts: 28
Joined: 2012-11-21T00:14:48-07:00
Authentication code: 6789

Re: Gap between clip-path and image content

Post by jd.wiese »

I did some more research on this and got some more information. After reading some other posts I think that the resulting PNG has only 1-bit transparency although 8-bit transparency should be possible (see https://www.imagemagick.org/discourse-s ... hp?t=24176).

But first off all some images to clarify the problem I have got :-)

This is the image I took for the following examples:
http://www.awg88.de/imagemagick/2016-05-26/43924200.tif

When I use Photoshop to remove the background I get smooth, semi transparent edges around the image:

Image

Putting the PNG on a black background gives the intended result:

Image

Then I use ImageMagick to create the PNG with the following command:

Code: Select all

convert 43924200.tif -alpha transparent -clip -alpha opaque -strip "PNG32:43924200.png"
The resulting PNG looks like this:

Image

Putting tis PNG on a black background gives this not -so-nice result:

Image

The edges between the image an the background are not smooth.

I found a solution for the problem in this post:

https://www.imagemagick.org/discourse-s ... hp?t=24176

In a first step the clip-path is extracted into a separate SVG-file:

Code: Select all

identify -format "%[8BIM:1999,2998:#1]" 43924200.tif > clip.svg
In a second step this SVG is used to remove the background from the image:

Code: Select all

convert 43924200.tif ( clip.svg -negate ) -alpha off -compose copy_opacity -composite "PNG32:43924200.png"
The resulting PNG has an 8-bit alpha channel and putting it on a background gives nice smooth edges between the image and the background. Mission accomplished! :D

The question is: Is there an easier way to achieve this without extracting the path first?
Last edited by jd.wiese on 2016-05-28T14:03:30-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gap between clip-path and image content

Post by snibgo »

Are you saying that v6 gives the correct result but v7 doesn't? If so, I suggest you submit a bug report. It should contain an easily reproducible case that clearly shows the problem.
snibgo's IM pages: im.snibgo.com
jd.wiese
Posts: 28
Joined: 2012-11-21T00:14:48-07:00
Authentication code: 6789

Re: Gap between clip-path and image content

Post by jd.wiese »

The result in V6 was a little better. Not much, but noticable in direct comparison. The edges were not semi-transparent, but closer to the image. More of the white pixels around the image from the original background were removed. I think thats the way the simple "-alpha transparent -clip -alpha opaque" action works in V6 and V7. The result was slightly better in V6 but I wouldn't say that there is an error in V7.

I found a solution that produces a perfect result, but that solution takes two steps (see above post). And that makes it difficult to integrate in our workflow. So I am searching for a way to combine the two steps into one convert-command.

JD
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Gap between clip-path and image content

Post by fmw42 »

try this, though you still need to extract the clip path

Code: Select all

identify -quiet -format "%[8BIM:1999,2998:#1]" 43924200.tif  | convert -quiet 43924200.tif ( - -negate ) -alpha off -compose copy_opacity -composite PNG32:43924200.png
I assume you can pipe in Windows. It works on a Mac (IM 6.9.4.4 Q16)

I also tried:

Code: Select all

convert -quiet 43924200.tif -alpha transparent -clip -alpha opaque -strip PNG32:43924200b.png
And then did a compare

Code: Select all

compare -metric rmse 43924200.png 43924200b.png null:
883.802 (0.013486)

The result is small, but could be better. I am not sure why the difference.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Gap between clip-path and image content

Post by fmw42 »

I believe the original code (from Anthony's pages on clip-path is disabling the anti-aliased pixels and using a binary clipping path; whereas your modified code is keeping the anti-aliases alpha values from the clip-path . This appears to be what is happening if I add +dither -colors 256 and look at the histogram. Your result has grayscale alpha, but the orginal result is binary alpha.
jd.wiese
Posts: 28
Joined: 2012-11-21T00:14:48-07:00
Authentication code: 6789

Re: Gap between clip-path and image content

Post by jd.wiese »

The solution works perfekt and could easily be integrated in our workflow. So thats the way I will go.

Thanks a lot!

JD
Post Reply