37

To solve Android build issue I need to replace all intermediate alpha pixel with solid pixel (leaving transparent background as is).

How to that with ImageMagick or other-command line tool to all images in a tree?

Image bg_all_block.9.png

 bg_all_block.9.png

Image btn_bg_common_press.9.png

btn_bg_common_press.9.png

enter image description here

UPDATE: I have found that I can detect if alpha is used, as in Detect Alpha Channel with ImageMagick

Other found links

Paul Verest
  • 60,022
  • 51
  • 208
  • 332

3 Answers3

106

To remove the alpha channel from single image use this command:

convert input.png -alpha off output.png

To remove the alpha channel from all images inside a folder, make use find to first find all PNG files, and then run 'm through convert:

find . -name "*.png" -exec convert "{}" -alpha off "{}" \;

Please test on a COPY of your files to be sure.

...

see dialog below, and the answer is based on that "we need to remove alpha that is not 255"

convert input.png -channel A -threshold 254 output.png

and for batch

mkdir batch
FOR %G IN (*.png) DO convert %G -channel A -threshold 254 batch\%G
Bramus
  • 1,732
  • 14
  • 19
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 2
    I don't do much Windows but it's probably `mogrify -alpha off *.png`. Not sure if `mogrify` has a `-r` switch for recursing through directories or if it will take `*/*.png` – Mark Setchell Mar 09 '15 at 08:06
  • Well, I found that this also removes transparent background for my icons. While I need only process those pixels with intermediate alpha... http://www.imagemagick.org/Usage/color_basics/#replace hints at some `convert balloon.gif -fill white -opaque blue balloon_white.gif` Now looking at what `-opaque means` – Paul Verest Mar 09 '15 at 08:09
  • Try `-channel A -threshold 254` – Mark Setchell Mar 09 '15 at 08:39
  • Can you post an image? – Mark Setchell Mar 09 '15 at 09:08
  • That is clever to say "we need remove alpha that is not 255". I tried with `convert bg_all_block.9.png -channel A -threshold 254 -alpha off bg_all_block_254.9.png`, but it changes background – Paul Verest Mar 09 '15 at 09:13
  • Yes, this is btn_bg_common_press.9.png – Paul Verest Mar 09 '15 at 09:17
  • Sorry, I meant for the `-channel A -threshold 254` to fully replace the `-alpha off`, so like this `convert input.png -channel A -threshold 254 output.png` – Mark Setchell Mar 09 '15 at 09:20
  • You might get on better experimenting with different percentages, like this `convert input.png -channel A -threshold 30% output.png` – Mark Setchell Mar 09 '15 at 09:25
  • Thank you so much. I have put final result into updated answer and accepted it. ImageMagick is so huge. – Paul Verest Mar 09 '15 at 09:29
  • 1
    My pleasure, glad to have been of assistance - sorry I was a bit slow to get to the right solution today. – Mark Setchell Mar 09 '15 at 09:33
  • For macOS users - first you have to install imagemagick. You can do this using brew: `brew install imagemagick` – bartektartanus Sep 02 '18 at 10:03
  • When I run `convert input.png -alpha off output.png`, I get an image that is all black. How do I convert transparency to white? I tried `convert input.png -background white -alpha off output.png` with no visible effect. – Ansa211 Mar 10 '19 at 06:56
  • 3
    But this one works: `convert input.png -background white -alpha remove -flatten -alpha off output.png` https://stackoverflow.com/a/28534287/2374478 – Ansa211 Mar 10 '19 at 07:11
  • convert: no images defined `./0.png' @ error/convert.c/ConvertImageCommand/3300. convert: no decode delegate for this image format `PNG' @ error/constitute.c/ReadImage/556. – kensai Jun 16 '19 at 07:01
4

What worked for me on macOS for batch processing was:

for f in *.png; do convert "$f" -channel A -threshold 254 "${f%%.png}.png"; done
Etherlind
  • 311
  • 2
  • 8
1

To remove alpha channel from all pictures in the folder (f.ex. all .png files) I use following command (in terminal on macOS):

for file in *.png; do convert $file -alpha deactivate; done

Unfortunately, none of any other solution given in this thread worked for me.

lukszar
  • 1,252
  • 10
  • 13