Page 1 of 1

Trim does not remove all edges as expected

Posted: 2013-06-07T04:12:53-07:00
by mgs
There is a SVG, containing a red grid and a black ellipse. The grid is replaced with transparent, then the image is trimmed. I would expect that only the ellipse is left over. However, also some transparent background at the top and at the left side survives. Why?

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 15.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 width="512px" height="512px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<g id="svgGrid">
	<line fill="none" stroke="#ff0000" x1="32" y1="0" x2="32" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="64" y1="0" x2="64" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="96" y1="0" x2="96" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="128" y1="0" x2="128" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="160" y1="0" x2="160" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="192" y1="0" x2="192" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="224" y1="0" x2="224" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="256" y1="0" x2="256" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="288" y1="0" x2="288" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="320" y1="0" x2="320" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="352" y1="0" x2="352" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="384" y1="0" x2="384" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="416" y1="0" x2="416" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="448" y1="0" x2="448" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="480" y1="0" x2="480" y2="512"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="32" x2="512" y2="32"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="64" x2="512" y2="64"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="96" x2="512" y2="96"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="128" x2="512" y2="128"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="160" x2="512" y2="160"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="192" x2="512" y2="192"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="224" x2="512" y2="224"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="256" x2="512" y2="256"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="288" x2="512" y2="288"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="320" x2="512" y2="320"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="352" x2="512" y2="352"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="384" x2="512" y2="384"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="416" x2="512" y2="416"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="448" x2="512" y2="448"/>
	<line fill="none" stroke="#ff0000" x1="0" y1="480" x2="512" y2="480"/>
	<rect x="0" y="0" fill="none" stroke="#ff0000" width="512" height="512"/>
</g>
<ellipse cx="256.25" cy="352.25" rx="224.25" ry="128.25"/>
</svg>

Code: Select all

convert -background transparent -define png:format=PNG32 -density 720 test-inp.svg -transparent "#ff0000" -trim -repage 0 test-out.png

Re: Trim does not remove all edges as expected

Posted: 2013-06-07T10:48:30-07:00
by GreenKoopa
The red lines will not necessarily be 100% opaque red on a 100% transparent background. The edges may transition. Why this only affects two sides of your image is chance. Change your density and this will change.

So setting #ff0000 to transparent is not enough. You need to deal with the entire spectrum of semi-transparent red (#ff0000??).

Re: Trim does not remove all edges as expected

Posted: 2013-06-07T22:17:32-07:00
by mgs
I looked at the image before #ff0000 is replaced with transparent.

Code: Select all

convert -background transparent -define png:format=PNG32 -density 720 test-inp.svg                                        test-1.png
convert -background transparent -define png:format=PNG32 -density 720 test-inp.svg -transparent "#ff0000" -trim -repage 0 test-2.png
There is no transition to be seen. These are sharp lines. Also in the final test-2.png there is only the ellipse with transparency around it.

UPDATE

I also played with -density and -fuzz. However, no success.

I finally found the reason, why the image is not trimmed as expected. There is one single faint horizontal line in the 5th pixel row from the top, which I had not seen before. I do not know enough about image processing and ImageMagick. So I cannot say, why this single row out of a 16x16 grid with 10 pixel wide lines appears at all.

Re: Trim does not remove all edges as expected

Posted: 2013-06-08T01:25:35-07:00
by GreenKoopa
mgs wrote:There is one single faint horizontal line in the 5th pixel row from the top, which I had not seen before. I do not know enough about image processing and ImageMagick. So I cannot say, why this single row out of a 16x16 grid with 10 pixel wide lines appears at all.
It appears to be a bug in rendering svg rectangles.

Re: Trim does not remove all edges as expected

Posted: 2013-06-08T02:21:00-07:00
by GreenKoopa
I think we have this problem cornered so I created a new topic on the Bugs forum. If you need further help with this bug, post your IM version, platform, and any information you think is relevant there. If you need help with a work-around, feel free to continue this topic.

ImageMagick < Bugs < svg rect
viewtopic.php?f=3&t=23562

Re: Trim does not remove all edges as expected

Posted: 2013-06-08T07:00:00-07:00
by mgs
Thanks a lot for the answer. Just for reference, my environment was Windows 8, IM 6.8.5-9 2013-05-30 Q16.

If somebody has another way to remove those gridlines, I would like to hear it. However, this would only be out of curiosity and for learning IM. I already solved my problem by doing some preprocessing on the SVG (reading and writing it as XML).