Q: Help needed with conversion of png files (adding opacity)

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
helpfux
Posts: 3
Joined: 2011-05-01T10:25:22-07:00
Authentication code: 8675308

Q: Help needed with conversion of png files (adding opacity)

Post by helpfux »

Hello
I really need your help with a problem, which I am not able to solve by myself. I tried the whole day now, but I did not succeed. :(
I have uploaded two png image files within the following ZIP file: http://www.2shared.com/file/OhEEo0ud/Images.html

The image names are:
17113_start.png
17113_final.png

The 17113_start.png file has an transparent background but the opacity of the other image parts is 100%. Now I would like to convert this file in such a way that the background is still transparent, but the other image parts should have an opacity value of 30% (transparency of 70%). This is shown in 17113_final.png. I have done this by using gimp, but as I want to do that with thousands of images it would be good if this is possible by using mogrify.exe. But I did not find the correct swith to do that. I thing also the image depth has to be altered, because even in Gimp it was only possible after switching the color model to RGB (24bit). But as I have said, I have no clue which commands I have to use for that.

Maybe someone could help me with this.
Many thanks in advance and best regards,
Chris
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Q: Help needed with conversion of png files (adding opac

Post by fmw42 »

try this, which works for me on IM 6.6.9.6 Q16 Mac OSX Tiger.

convert 17113_start.png -channel a -evaluate multiply 0.7 +channel 17113_start_proc1.png

you may want to adjust the -evaluate multiply 0.7 either higher or lower to match your other result.

If you have many files to change and they all need the same adjustment, then you can do them all at once using mogrify. I suggest creating two directories. The first to hold your old images and the second to hold the new ones. call them 17113a and 17113b.

cd 17113a
mogrify -path /Users/fred/17113b -format png -channel a -evaluate multiply 0.7 +channel *.png
helpfux
Posts: 3
Joined: 2011-05-01T10:25:22-07:00
Authentication code: 8675308

Re: Q: Help needed with conversion of png files (adding opac

Post by helpfux »

Thank you for your help fmw42. With your help I managed to set up a proper command line. I added some things and ended with this command:

mogrify.exe -fuzz 30%% -transparent none -fuzz 0%% -channel a -evaluate multiply 0.3 +channel file.png

So far this works great. Thank you for your fast help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Q: Help needed with conversion of png files (adding opac

Post by fmw42 »

helpfux wrote:Thank you for your help fmw42. With your help I managed to set up a proper command line. I added some things and ended with this command:

mogrify.exe -fuzz 30%% -transparent none -fuzz 0%% -channel a -evaluate multiply 0.3 +channel file.png

So far this works great. Thank you for your fast help.

The second fuzz (-fuzz 0%%) should not be needed.

Also file.png will only get one file named file.png. If you want more, then you need to use the * wild card, though I am not sure if that works on windows. But see http://www.imagemagick.org/Usage/windows/

Also see mogrify at http://www.imagemagick.org/Usage/basics/#mogrify
helpfux
Posts: 3
Joined: 2011-05-01T10:25:22-07:00
Authentication code: 8675308

Re: Q: Help needed with conversion of png files (adding opac

Post by helpfux »

Thank you. I implemented the command line in a batch script and replaced file.png with a variable. I just added it here for a better readability. I added the fuzz 0% to be sure that this dos not affect any following command.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Q: Help needed with conversion of png files (adding opac

Post by anthony »

fmw42 wrote:
helpfux wrote:Thank you for your help fmw42. With your help I managed to set up a proper command line. I added some things and ended with this command:

mogrify.exe -fuzz 30%% -transparent none -fuzz 0%% -channel a -evaluate multiply 0.3 +channel file.png

So far this works great. Thank you for your fast help.

The second fuzz (-fuzz 0%%) should not be needed.

Also file.png will only get one file named file.png. If you want more, then you need to use the * wild card, though I am not sure if that works on windows. But see http://www.imagemagick.org/Usage/windows/

Also see mogrify at http://www.imagemagick.org/Usage/basics/#mogrify
The second fuzz does not hurt, and probably a good thing if the above gets embedded in a longer construct. On feature I plan to add in IM v7 is a -reset option that will reset all working settings to defaults. This is becoming a necessity as commands get longer and problems of old settings interacting with later commands becomes more common.

However be warned that -fuzz 30% -transparent none only works correctly for IM v6.6.6-4. before that Blacks were 'closer' to 'none' than whites! At that version the distance calculator was fixed to correctly handle transparency.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Q: Help needed with conversion of png files (adding opac

Post by fmw42 »

mogrify.exe -fuzz 30%% -transparent none -fuzz 0%% -channel a -evaluate multiply 0.3 +channel file.png
I am rather puzzled why you (or anyone) would use -fuzz XX% -transparent none?

Usually one uses -transparent to turn some color into transparent, such as -transparent white, will make white into transparent. If you already have transparency, what do you buy by using -transparent none. If you want to expand the range of transparency adding -fuzz ... -transparent none seems a strange way to do that rather than expanding the fuzz value for -transparent somecolor.

Perhaps you could explain what it is you are really trying to do. Your original question did not seem to relate to this kind of situation.

Please also note Anthony's last comment above:
Anthony wrote:However be warned that -fuzz 30% -transparent none only works correctly for IM v6.6.6-4. before that Blacks were 'closer' to 'none' than whites! At that version the distance calculator was fixed to correctly handle transparency.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Q: Help needed with conversion of png files (adding opac

Post by anthony »

fmw42 wrote:
mogrify.exe -fuzz 30%% -transparent none -fuzz 0%% -channel a -evaluate multiply 0.3 +channel file.png
I am rather puzzled why you (or anyone) would use -fuzz XX% -transparent none?
I was puzzled by it does a short time too. But it is valid. What it does is make anything that is withing 30% of fully transparent, fully transparent. That is it removes all the high transparency pixels merging them to be full transparency, and specifically fully-transparency black. It is like using -black-threshold on alpha values (though because IM uses matte values it is really more like -white-threshold), however it also assigns a black color as well.

Remember any semi-transparent pixel has valid color. making them fully-transparent removes that need, allowing you to make more pixels fully-transparent-black (or "none" color).

The result is the image will... 1/ the image will 'trim' better. 2/ it will PNG compress better.
See Fuzz Factor and transparency
http://www.imagemagick.org/Usage/color_ ... fuzz_alpha


A note about this has been added to the PNG section. See (in a few hours)
http://www.imagemagick.org/Usage/formats/#png_compress

Actually I found using -level with some minor gamma correction works better!

Even so using -alpha background to set the color behind the transparency may be a good idea
See. -alpha background
http://www.imagemagick.org/Usage/maskin ... background
It will not help compression any more, but it does make the older IE v6 work better with regard to PNG images! See PNG, Web Browsers and Transparency
http://www.imagemagick.org/Usage/formats/#png_www
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply