Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imagemagick: remove alpha component (replace all intermediate alpha pixel with solid pixel)

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

  • https://graphicdesign.stackexchange.com/questions/16120/batch-replacing-color-with-transparency
  • http://www.imagemagick.org/Usage/color_basics/#replace
like image 991
Paul Verest Avatar asked Mar 09 '15 07:03

Paul Verest


3 Answers

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
like image 115
Mark Setchell Avatar answered Oct 24 '22 07:10

Mark Setchell


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
like image 37
Etherlind Avatar answered Oct 24 '22 06:10

Etherlind


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.

like image 1
lukszar Avatar answered Oct 24 '22 06:10

lukszar