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

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
Image btn_bg_common_press.9.png
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

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

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

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.

Related

ImageMagick: Mass extract alpha from png \ Combine files back

I can extract alpha from 1 file at time with this command
convert Alpha.png -alpha extract Alpha_alpha2.png
How to use it with .bat to mass extract all png in folder?
And what command would be to add alpha back to image?
You could use mogrify to extract the alpha channel from all images in a folder in Imagemagick.
mogrify -path path_to/new_folder -format png -alpha extract *.png
That would put all the alpha channels into an empty (but existing) new folder with the name of the input image
To add an alpha channel onto an image, one at a time, you would do:
convert image.png alpha.png -alpha off -compose copy_opacity -composite result.png

ImageMagick removes colors when alpha is 0 (png)

I am converting many 3D textures with imagemagick for a video game. My source files are png, my target files are png, too. But I notice that whenever the alpha channel drops to 0.0 my color information are gone (and I need them). I just want to scale all channels as they are. I guess there is a small switch that fixes that problem, but the deadline is near and I cannot find anything about that.
Simple command to reproduce this:
convert source-with-alpha.png -scale 2014 target.png (I also tried -resize and it also didn't work).
Doing just convert source-with-alpha.png target.png works fine though (but has no scaledown).
Thank you for your help.
I guess ImageMagick is trying to optimise something but not sure what/why. Maybe the idea is that if something is transparent you can't see it, so we might as well make it black so it compresses well.
Anyway, try separating the channels so they are all just treated as independent channels, then resizing and recombining:
convert input.png -channel RGBA -separate -resize XxY -combine result.png
I am not sure I understand your problem. I have no issue resizing a transparent PNG image with ImageMagick 6.9.10.28 Q16 Mac OSX with libpng 1.6.36. Perhaps you need to upgrade one or both.
Image:
Make white into transparent:
convert logo.png -transparent white logot.png
Resize it:
convert logot.png -resize 25% logot_small.png
I tried Mark Setchell answer with two different versions of Windows imagemagick but I still have this issue.
RGB becomes 0 if alpha is 0 when resizing.
A workaround was to add alpha a little bit so it becomes non-zero:
magic.exe input.tga -channel a -evaluate add 0.2% -channel RGBA -separate -filter Quadratic -resize -resize XxY! -combine result.tga
or also (same result)
magick.exe ( input.tga -alpha off -filter Quadratic -resize XxY! ) ( input.tga -filter Quadratic -resize XxY! -alpha extract -evaluate add 0.2% ) -compose Copy_Alpha -composite result.tga
("-filter Quadratic" is optional)
Post one of your tga files so we can test with it. What is your ImageMagick version? There should be no need for any switch. This works fine for me on IM 6.9.10.65 Q16 Mac OSX.
Make a transparent TGA:
convert logo: -transparent white logo.tga
transparent tga image
Resize by 50%
convert logo.tga -resize 50% logo2.tga
resized transparent tga image

Imagemagick convert and compose batch of files

I am trying to make a fast convert of a batch of files like this:
convert ./src/*.png -set filename: '%t' -gravity West -draw 'image over 0,0 424,600 "./panel.png"' ./dest/%[filename:].png
which is pretty similar to COMPOSITE:
convert ./src/*.png ./panel.png -set filename: '%t' -gravity +0+0 -composite ./dest/%[filename:].png
except the last one is not working and just making one first crappy-looking file.
Looks like it's bug?
Does anybody know how to make it more correct with -composite?
for|awk|ls|find for each file in shell is not acceptable - because that is slower than the first example.
Read in the list of files,
set their output filenames,
include the IM special image placeholder "null:",
read in your overlay image,
optionally, set the geometry,
and composite that overlay onto all the other images with "-layers composite".
That null: separates the original input file list from the overlay image so ImageMagick knows where in the stack you want to start doing the composite.
Try something like this (one step per line for readability):
convert ./src/*.png \
-set filename: '%t' \
null: \
./panel.png \
-layers composite ./dest/%[filename:].png
You could use Imagemagick mogrify command. See http://www.imagemagick.org/Usage/basics/#mogrify and http://www.imagemagick.org/Usage/basics/#mogrify_compose
cd to input directory
mogrify -format png -path ./dest -gravity West -draw 'image over 0,0 424,600 "./panel.png"' *.png
Looks like it's bug?
Not a bug. Your second command is telling ImageMagick to consume all files matched into an image stack, and composite it as one.
You can attempt the same solution with the mogrify utility, but I believe it would be way simpler if you expand the bash script with a single for loop.
for f in $(ls src/*.png)
do
dest=$(basename $f);
convert "$f" ./panel.png -gravity West -composite "./dest/$dest"
done

Adding border to multiple images using gimp?

I have around 100 pictures that i want to add white border to it all at once.
I use Linux and also use gimp ,.. please suggest me something to do so online of offline.
and one more thing that i have tried convert option on imagemagick but nothing happen.
If you want to do 100 all at once you will be best off using ImageMagick's mogrify command like this to add a 10 pixel white border around all images:
mogrify -mattecolor white -frame 10x10 image*.jpg
If the images are not all in a single directory, you can do the following which will do the same thing across all subdirectories of the one you are currently in:
find . -name \*.jpg -exec convert "{}" -mattecolor white -frame 10x10 "{}" \;
Obviously you can change the 10 to a different number of pixels if you wish.
Please make a backup before using this as I may have misunderstood your needs.
Updated
If you want a drop shadow, you really need to be working with PNG rather than JPG since the former supports transparency and the latter doesn't - but IM can convert your JPEGs to PNGs anyway. I use the following command for drop shadows:
convert image.jpg \( -clone 0 -background black -shadow 80x3+0+8 \) -reverse -background none -layers merge +repage image.png
So, I would apply that to a pile of images like this:
#!/bin/bash
for f in *.jpg; do
new=${f%%jpg}png # Work out new name = original name minus "jpg" + "png"
echo Processing $f into $new
convert "$f" \( -clone 0 -background black -shadow 80x3+0+8 \) -reverse -background none -layers merge +repage "$new"
done

ImageMagick: write pixels in BGR order

I want to convert (a lot of) JPEG images to the Sun Raster format (see here for instance), because my program can only use this format.
The issue is that my program only allows images of type Old
or Standard i.e. with pixel written in the BGR order. (I should change that but don't have the time).
By default, ImageMagick generates files in the RGB format.
I have found a trick to swap the color planes, but as the file is style written as RGB I get fancy colors.
This is probably a simple option to pass to ImageMagick, but I didn't find it.
Do you have an idea?
Another way to swap colors is to use the -color-matrix option. For example, to convert RGB to BGR:
convert input.png -color-matrix '0 0 1
0 1 0
1 0 0' output.png
Reference: http://www.imagemagick.org/Usage/color_mods/
In Imagemagick 6, you can do that easily with -swap command. Lets swap the red and blue channels in the logo image.
convert logo.png -separate +channel \
-swap 0,2 -combine -colorspace sRGB logo_bgr.png
For very old versions of Imagemagick use RGB rather than sRGB.
In Imagemagick 7, use magick rather than convert.
I have an idea how to swap Red and Blue colors in RGB image:
convert ( image_RGB.bmp -channel B -separate ) \
( image_RGB.bmp -channel G -separate ) \
( image_RGB.bmp -channel R -separate ) -channel RGB -combine image_BGR.bmp
You just cut channels form original image and write them in reverse order.
I would have thought that in Imagemagick
convert image.jpg image.sun
or
convert image.jpg SUN:image.sun
would convert to BGR format if that is the standard for the sun raster format. See imagemagick.org/script/formats.php.
Have you tried that?
If that does not work, then use my -swap command but write to .sun file format rather than png, which I used above. That is
convert logo.png -separate +channel -swap 0,2 -combine -colorspace sRGB logo_bgr.sun

Resources