Page 1 of 1

Why does Colorize also change the transparent background?

Posted: 2013-06-06T06:36:34-07:00
by mgs
With 6.8.5-9 2013-05-30 Q16, my first steps with IM.

I want to take an SVG as input. It has a transparent background and a black icon. I want the following.
  • - the padding around the icon is to be removed
    - the resulting width should be 64px
    - the black icon should be made gray
    - the transparent areas should be left unchanged
I tried as follows.

Code: Select all

convert -background transparent -density 720 test.svg -trim                             -resize 64 test1.png
convert -background transparent -density 720 test.svg -trim -fill white -colorize "50%" -resize 64 test2.png
The first test (without making the icon gray) runs fine. However, the seond test does not work as expected: the transparent background is also made gray.

In http://www.imagemagick.org/Usage/color_mods/#colorize I read the following. So I do not understand what to do for keeping a transparent background.
The alpha channel of the original image is preserved
Second minor question: is the combination of "-density" and "-resize" the correct way for creating a specific-width PNG from an SVG?

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" baseProfile="full" width="76" height="76" viewBox="0 0 76.00 76.00" enable-background="new 0 0 76.00 76.00" xml:space="preserve">
	<path fill="#000000" fill-opacity="1" stroke-width="0.2" stroke-linejoin="round" d="M 56.1355,32.5475L 43.4466,19.8526C 42.7886,20.4988 42.298,21.2123 41.9749,21.9932C 41.6519,22.7741 41.4903,23.5729 41.4903,24.3895C 41.4903,25.1942 41.6529,25.987 41.9779,26.7679L 34.0577,34.6821C 33.3918,34.3372 32.6991,34.0776 31.9796,33.9032C 31.2601,33.7288 30.5298,33.6415 29.7885,33.6415C 28.623,33.6415 27.4953,33.8526 26.4052,34.2748C 25.315,34.697 24.3419,35.3342 23.4856,36.1865L 30.2344,42.9174L 25.9027,47.9032L 22.6532,51.8425L 20.5988,54.5836C 20.1212,55.2892 19.8823,55.753 19.8823,55.975L 19.8645,56.0701L 19.9002,56.088L 19.9002,56.1474L 19.9358,56.1058L 20.0131,56.1236C 20.2351,56.1236 20.6989,55.8888 21.4045,55.419L 24.1457,53.3765L 28.0849,50.1151L 33.0945,45.7775L 39.8016,52.5025C 40.6579,51.6462 41.2961,50.6731 41.7163,49.5829C 42.1365,48.4928 42.3466,47.367 42.3466,46.2056C 42.3466,45.4603 42.2603,44.729 42.0879,44.0115C 41.9155,43.294 41.6548,42.6003 41.3069,41.9304L 49.2202,34.0161C 50.0011,34.3372 50.7939,34.4978 51.5986,34.4978C 52.4192,34.4978 53.2189,34.3362 53.9979,34.0132C 54.7768,33.6901 55.4894,33.2015 56.1355,32.5475 Z "/>
</svg>

Re: Why does Colorize also change the transparent background

Posted: 2013-06-06T10:20:05-07:00
by fmw42
try

convert -background transparent -density 720 test.svg -trim -alpha off -fill white -colorize "50%" -alpha on -resize 64 test2.png

Re: Why does Colorize also change the transparent background

Posted: 2013-06-06T10:44:35-07:00
by mgs
Thanks a lot. That works perfectly.

Re: Why does Colorize also change the transparent background

Posted: 2013-06-06T10:45:17-07:00
by GreenKoopa
First I tried to fix this by using -channel. That didn't work, making this seem even more like a bug, but I don't use -colorize so I'm not sure. Since you are colorizing with white, I would have used +level. Relpace
-fill white -colorize 50%
with (to brighten to a minimum 50%)
+level 50,100%
or (to force to 50% gray)
+level 50,50%

Or fmw42's solution will work great too. Or there are many other solutions depending on your general case.

You may want a +repage after -trim.
mgs wrote:Second minor question: is the combination of "-density" and "-resize" the correct way for creating a specific-width PNG from an SVG?
This is a very reasonable method. The only way to avoid the -resize would be to calculate the perfect initial density, either on paper or in a simple script.

Re: Why does Colorize also change the transparent background

Posted: 2013-06-06T12:24:06-07:00
by mgs
Thanks a lot for the answer.

In the meantime I have also "discovered" the "-clut" operator, which I have successfully used for changing the color of monochrome images.

Re: Why does Colorize also change the transparent background

Posted: 2013-06-06T14:55:31-07:00
by fmw42
You can also use +level-colors with two colors.

see all the color modification methods at
http://www.imagemagick.org/Usage/color_mods/

Re: Why does Colorize also change the transparent background

Posted: 2013-06-25T08:52:44-07:00
by frankoid
http://www.imagemagick.org/Usage/color_mods/#colorize says that:
One common use of the "-colorize" operator is to simply replace all the colors in an existing image (tinting '100%') to set the color of a shape mask.

convert test.png -fill blue -colorize 100% colorize_shape.png
However, if I download test.png from that page and then run that command (convert test.png -fill blue -colorize 100% colorize_shape.png) then colorize_shape.png ends up completely blue (even the transparent parts are coloured blue).

This is with version 6.8.5-5 of ImageMagick. Is it a bug that this version of ImageMagick behaves differently than what is described on http://www.imagemagick.org/Usage/color_mods/#colorize? I think that previous versions of ImageMagick used to behave as described - I wrote a program in 2010 that issues similar commands and it used to work as described in http://www.imagemagick.org/Usage/color_mods/#colorize for creating shape masks but now I end up with all-one-colour images as output.

I'm currently using fmw42's suggestion of adding '-alpha off' and '-alpha on' to the command line as a workaround, but I'm curious about why ImageMagick's behaviour has changed (was it deliberate?).

Re: Why does Colorize also change the transparent background

Posted: 2013-06-25T10:04:27-07:00
by fmw42
I don't know if it was intentional or not. It did get changed at one point. Thus the need for my alpha channel solutions. There has been debate about which way is more correct from the definition of what -colorize means. But it was apparently decided to leave as is and not revert back again. Further information would need to come from the IM developers.

Re: Why does Colorize also change the transparent background

Posted: 2013-06-26T17:17:50-07:00
by anthony
I thought it was decided to revert!

I know a lot of things depend on it.

If it was 'channel' controled, then the defult in IMv6 would be the older behaviour.

I would like to see a 'channel' output control for the 'blend' and 'dissolve' composition operators, as a lower level method of doing a image colorize.

Re: Why does Colorize also change the transparent background

Posted: 2013-06-26T17:52:46-07:00
by fmw42
anthony wrote:If it was 'channel' controled, then the defult in IMv6 would be the older behaviour.
channel controlled makes no sense to me for -colorize. What would the meaning of -fill red -channel red -colorize be? How do you make one channel red?

However, since one has alpha control, it makes sense to make it whatever color one wants and use -alpha off and on to preserve the alpha channel.

But it could be the opposite and one could just use -alpha opaque or -alpha off if one wanted the result to be color without the alpha.

As it is now, one has full control by using -alpha. So why revert again. It has been this way for quite some time now.

Is there something special that the older way does that cannot be done with -alpha control and the current approach? Or is this just a philosophical difference of opinion?