ImageMagick: Mass extract alpha from png \ Combine files back - image-processing

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

Related

How to fuse 2 image and keep the same filename / overwrite the original

I have 3 png image: a red square with a transparent background (1.png), a map (PLAN.png) and a color gradient with a transparent background (LEGEND.png). I want to first put the red square atop the map and then put the color gradient atop of it, while keeping the original filename of the red square for multiple files in multiple folders
Here's what I came up with in the windows cmd:
cd [location of the folder]
magick convert ../PLAN.png 1.png -compose multiply -composite 1.png && magick composite ../LEGEND.png 1.png 1.png
The problem is that for each file, I need to manually change 1.png to for example 1 2 3.png . I had used this command:
magick mogrify -gravity center -draw "image Over 0,0 0,0 '../PLAN2.png'" *.png
While with it I don't have to manually manually change the 1.png, it doesn't work with the map since it put it atop of the red square
In ImageMagick 7, do the following. Note, use magick, not magick convert.
magick PLAN.png 1.png -compose over -composite LEGEND.png -compose over -composite 1.png
or
magick PLAN.png 1.png LEGEND.png -background none -flatten 1.png
Do either of those do what you want?

imagemagick mean image sequence and ignore alpha channel

I want to get the mean of a sequence of images by using Imagemagick. Therefore I use the following command:
convert *.png -evaluate-sequence mean MEAN.png
Each of my images does contain an alpha channel. What I want is: Combine all the images by ignoring the alpha channel.
When I combine the images, the alpha channel is considered in the "mean" method and my final image has transparency. That isn't what I want.
Result:
I tried to add the parameter -alpha off, but then Imagemagick converts the alpha channel to black.
convert *.png -alpha off -evaluate-sequence mean MEAN.png
Result:
Photoshop does it right. I load all images in a stack and create a smart object. When I use the "mean" method in Photoshop, the alpha channel is not considdered in the final result.
Result that I want with Imagemagick:
Does someone have an idea how to do that with Imagemagick?
What you need to do is to use the alpha channels as weighting images for each image. The total fraction of white values at each pixel from all the alpha channels would be the weight to use for the average. So something like this should do what you want or at least be close.
First, cd to your directory of images. Then run the following.
convert *.png -evaluate-sequence mean \
\( -clone 0 -alpha off \) \
\( -clone 0 -alpha extract \) \
-delete 0 +swap -compose divide -composite result.png
This will work if there is some image texture at each pixel coming from al least one image. That is at a given pixel all images are not totally black (transparent).
compare -metric rmse result.png mean_photoshop.png null:
125.167 (0.00190993)
So this shows that there is about 0.2% difference between my result and what you got from photoshop
Maybe this way of working will help you get there - or at least explain the problem:
convert xc:"rgba(255,0,0,1)" xc:"rgba(0,0,0,1)" xc:"rgba(0,0,0,0)" -depth 8 -evaluate-sequence mean txt:
Output
# ImageMagick pixel enumeration: 1,1,65535,srgba
0,0: (21845,0,0,43690) #550000AA srgba(85,0,0,0.666667)
Using IM 6.8.9.4 Q16 or IM 7.0.5.5 Q16 Mac OSX Sierra, this seems to work fine for me:
Make transparent image
convert logo: -transparent white logot.png
Get mean
convert logot.png logot.png logot.png -alpha off -evaluate-sequence mean result.png
magick logot.png logot.png logot.png -alpha off -evaluate-sequence mean result.png
This also seems to work:
convert logot.png logot.png logot.png -channel rgb -evaluate-sequence mean -alpha off result.png
So perhaps you need to upgrade your ImageMagick (and/or libpng?)
Can you post a zip file of some of your input images, so we can test with your images?
One problem that I see is that the PNG images that you provided have black under the transparent areas and not image texture. So when you disable alpha as in my commands above, you see black and the black gets averaged into the final result. Did you use these same PNG images in Photoshop or did you have Photoshop PSD images or some other images that you used and then exported to PNG, which may have put black under the transparent areas. Have you tried using the same PNG images in Photoshop to do the average?
In fact, you have 8-bit color (palette) images, which have one color (black) assigned to be the transparent color.

How I Can Composite image with imagemagick with this file?

When i want to composite one image with png file i use:
exec("convert 1.png ".$newfile1." -geometry +110+70 -compose DstOver -composite ".$result_image);
And when i want use Perspective i use :
exec('convert '.$newfile1.' -matte -background transparent -virtual-pixel background -distort Perspective "0,0 0,0 393,0 236,65 393,486 393,486 0,486 153,486" '.$newfile1.'');
But with this png file for example :
create Perspective file is very different and hard because i don't know the source image size and how create best perspective..
any help?
(soryy for my bad english)

Masking an image's alpha in Imagemagick

I need to create an image to be used as a rollover background image. It's a circular pattern that is split into 8 pieces. Here's a screengrab of the main image (png with transparency):
And here's a screengrab of the mask image. It's the same size as the main image and features 'pie' pieces in order to mask all but the sector that is being hovered over.
I'm including screengrabs, as I believe the answer should be pretty simple (aren't all answers simple when you know them?!) so I'll save bandwidth, but I can upload the original files if it's helpful.
Here's the command I'm using to create the new masked image:
convert main.png \( mask.png -colorspace gray -alpha off \) \
-compose copy-opacity -composite new.png
The trouble is that the new image created has flattened the original image's alpha to a black background:
How do I get Imagemagick to preserve the original png's transparency?
You want masked composition to do this. http://imagemagick.org/Usage/compose/#mask
The technique is to compose the original image (the src) onto a fully transparent image of the same size (the dst), using a mask to limit composition area (the mask). It is a special case of the -composite operator, and involves 3 images, rather than 2 like the rest of the compose methods. You don't specify any -compose mode for this.
A quick way to get the fully transparent dst that you need for this technique is to clone the src image and zero out its alpha channel, then swap the order of src and dst so that they are in the right order for the -composite operation to follow:
convert main.png -alpha on \( +clone -channel a -fx 0 \) +swap mask.png -composite out.png
I was not satisfied with retroj's solution as it seems to ignore the grayscale of the mask.
This one worked for me:
composite -compose Dst_In \( mask.png -alpha copy \) main.png -alpha Set PNG32:result.png
or
convert -compose Dst_In \( mask.png -alpha copy \) main.png -alpha Set -composite PNG32:result.png
Dst_In method multiplies the alpha channels of two images. The trick here is to convert the grayscale mask to an alpha channel for it which is done with -alpha copy.

How to combine RGB image and 8-bit alpha image into single PNG file with Imagemagick?

Imagine I have rgb.jpg and alpha.png. How to combine them into rgba.png with Imagemagick?
Since you didn't specify, I'll assume a sufficiently new version of ImageMagick being run from the command line. This was tested using ImageMagick 6.6.9-7 on Linux (from Debian).
If alpha.png is an image from which you want to copy the alpha channel, or it is a greyscale image with no alpha channel, you can use the following:
convert rgb.jpg alpha.png -compose copy-opacity -composite rgba.png
Otherwise, you will have to do a bit more processing on alpha.png. For example, this converts alpha.png to greyscale and ignores the alpha channel:
convert rgb.jpg ( alpha.png -colorspace gray -alpha off ) -compose copy-opacity -composite rgba.png
Note that you may have to escape the parentheses in the latter command, depending on your command-line environment.
If necessary, you can also invert the greyscale image before using it as the alpha channel:
convert rgb.jpg ( alpha.png -colorspace gray -alpha off -negate ) -compose copy-opacity -composite rgba.png

Resources