How to add Alpha -channels en masse with ImageMagick? - imagemagick

Suppose a dir with a lot of images, some of them miss the alpha-channel so looking bad against a background. I thought every picture had alpha -channel but some pictures now show white instead of having transparency. How can I add the alpha channels en masse?
Not Working
$ convert imageNoAlpha.png -alpha on imageAlpha.png

This will change white to transparent as in you coment BUT any other white in the image will be transparent as well:
convert input.jpg -background none -transparent white -flatten output.png

Thanks to Bonzo, I was able to get very cool results even without anti-alias! You can see the difference in the eyes with the below picture. I don't know how much you lose information by this but at least I am more able to adjust things, not so easy in anti-aliasing at least for me.
$ convert in.png -background none -fuzz 10% -transparent white -flatten out.png
En masse -approach is probably easiest with a bash -for-loop or use ImageMagick -pkg built for python. I don't know which approach is better, I will leave this here for future answers to improve. Thanks.

Related

Apply imagemagick transformation on only part of an image, whilst keeping the rest "stock"?

I have many documents per day that are photographed and I need to organise by QR code. The problem is, zbarimg is struggling to pick up many of the QR codes in the photos, so I have been trialling processing them with imagemagick first, using morphology open, thesholding, etc, which has yielded much better results.
The only issue with this is these apply to the whole image, which makes the rest of the file unusable for me, as I deal with the rest of the image based on colours and information which all gets destroyed in the processing. Could anybody give me an example on how I could apply my imagemagick filters to only a part of an image (coordinate based is fine) and leave the rest of the image untouched, so I can continue my process? I will be applying this to all images in a folder, so it's a batch file running this for me in most instances.
I have tried using crops, however this obviously leaves me with only the cropped portion of the image, which doesn't actually help when trying to process the rest of the file.
I'm running my scripts on Windows 11, if that means anything in terms of the solution.
Many thanks!
Tom
EDIT:
Thank you all for the advice given!
I solved my problem using the following:
convert a.jpg ( -clone 0 -fill white -colorize 100 -fill black -draw "polygon 500,300 500,1500 1300,1500 1300,300" -alpha off -write mpr:mask +delete ) -mask mpr:mask +repage -threshold 50% -morphology open square:4 +mask c.jpg
I did post this as an answer, but (and I have no idea why, I'm brand new to stack exchange) my answer was deleted. I used the clone to make the mask with the coordinates needed, then added the threshold and morphology that would make my QR codes more legible!
Thanks again everyone, really helped me out on my journey to figure it out :D
You can use -region to specify a region to process. So starting with this:
You can then specify a region to colorise with blue and then change the region to blur part of the blue and part of the original:
magick swirl.jpg -region 100x100+50+50 -fill blue -colorize 100% -region 100x100+100+100 -blur x20 result.png
The solution using -region may be the most direct. In ImageMagick versions where -region is not supported the same result can usually be achieved by cropping and modifying a clone inside parentheses.
magick swirl.jpg ( +clone -crop 100x100+50+50 -fill blue -colorize 50 ) -flatten result.png
The cloned, cropped, and and modified piece maintains its original geometry, so the -flatten operation puts it back where it was on the input image after the parentheses.

Preserving PNG transparency during a simple transformation in imagemagick

Task: i have an input png file (many actually, but i'll just loop the solution). It is 16x16 PNG, 32bit with partial transparency along edges.
It so happens that toolbar of a certain stupid platform requires 17x17 files. My problem is that imagemagick kills transparency when doing simple transformations.
So:
Sanity check:
convert add.png PNG32:add_COPIED.png
creates another 16x16#32bpp file. So far so good.
Transformation (gravity is fine):
convert add.png -extent 17x17 PNG32:add_17.png
creates a file with solid white background. That's not good.
What doesn't work:
I tried a serious number of combinations of transparent, transparent-color, background, alpha and flatten. Got nowhere.
What does work:
convert address_book.png -alpha Extract address_book_MASK.png
convert address_book.png -extent 17x17 PNG32:address_book_17.png
convert address_book_MASK.png -background black -extent 17x17 address_book_MASK17.png
composite -compose CopyOpacity address_book_MASK17.png address_book_17.png PNG32:address_book_FIN.png
While i have a working set of commands and I can get through the day, I honestly believe that this is the wrong way to do things - four commands that create 3 intermediate files that i need to delete later. Surely it can be done in a better way?
Set the background colour before changing the extent:
convert input.png -background none -extent WxH 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 how to delete all colors expect black

I have question. Is it possible to delete all colors from image, but save the black color? I have a picture with several unknown colors. (Therefore I can't just replace e.g red color with white color). I have an image like this:
And I am trying to delete this "Text2" and "Text3". Is that possible? Which option in Imagemagick should I use?
Mark's answer is probably the best if you easily can separate the black region from the rest of the image, since it preserves the antialiased text better. However, if not then you can do something similar to Bonzo's command. Here is another variation of that.
convert EWwSX.jpg -fuzz 40% -fill white +opaque black result.png
Not sure I understand your question - you can't really delete a colour in an image. What would be left? I understand you can't replace the red with white because you have reds elsewhere in your image.
I guess the easiest thing to do is draw a white rectangle over the unwanted text:
convert text.jpg -fill white -draw "rectangle 20,72 100,150" result.jpg
Not a great result but with some tweeking or work you could improve it:
convert EWwSX.jpg -threshold 20% black.png

How to replace white background color with transparent of an image in ImageMagick?

I have an image in .jpg format with white background color. I want to remove the white background color to transparent in Imagemagick. I tried many ways but still the white background can not be removed. Can some one help me to solve this.
You cannot have transparent background colors in your JPEGs. The JPEG file format doesn't support transparency.
If you need transparent background, you need to convert the JPEG to
either PNG (high quality, filesize possibly larger than JPEG)
or GIF (in case you can tolerate low quality and a range of maximally 255 colors).
Example command:
convert your.jpg -transparent white your.png
First, you need to convert the image format from .jpg to .png format, because JPEG does not support transparency. Then use this command:
convert image1.png -fuzz 20% -transparent white result.png
The -fuzz option allows the specified percentage deviation from the pure white colour to be converted to transparent as well. This is useful, for example, when your image contains noise or subtle gradients.
I just found a very neat thing!
magicwand 1,1 -t 20 -f image -r outside -m overlay -o 0 image.jpg imgOutput.png
It is a Fred Weinhaus bash script that can be downloaded from here (for non commercial use only). Also there has about 250 scripts!! and this one is amazing! it did exactly the trick, to remove all background while keeping the inner image dots untouched!
At his page, there are several images as examples so you pick what you need to put on the command line!
The initial position 1,1 is a general guesser saying all the contour is background.
Pay attention that the output must be ".png"
This is my solution without magicwand (replace magick by convert for im < 7.0):
magick img.png -fuzz 20% -fill none -draw "alpha 1x1 floodfill" result.png
Get the background automatically and remove it :
bg=$(convert input.png -format "%[pixel:p{0,0}]" info:)
convert input.png -fuzz 20% -transparent "$bg" output.png

Resources