Imagemagick: Create 1 image out of 3 with luminance/overlay mask - imagemagick

I want to generate an image out of 3 images. One of these images is the background, one the shape mask and one the color of the shape.
Here are the images:
shape.png
(transparent background, white circle in the middle and black circle inside and gray circle inside the black one)
shapecolor.png
background.png
The background.png should be the overall background. On top of that is the shape and all white parts (and also the white in the gray parts) should be in the color of shapecolor.png
I used simple mono color images to make it easier but I use some textures in reality ^^
I have no idea how to solve this problem in Imagemagick, the tool is very powerful and the documentation is not so easy to understand. Tried to solve this for 3h, but did not get the result, which should look like this:
Can anyone help please?

Here is one way to do it in Imagemagick. In the second line, extract the alpha channel from omg.png and save it in an mpr: in-memory image and then delete the clone. Then I use the mpr: image later in the last step.
convert red.png img.png \
\( +clone -alpha extract -write mpr:alpha +delete \) \
-compose multiply -composite \
green.png +swap mpr:alpha -compose over -composite \
result.png

Related

Imagemagick: distorting images on top of background?

I've got one background image: sky.jpg
And two transparent PNG images: gradient.png and tree.png
Now I want to draw the two images on the background with a perspective distortion, like this:
The destination coordinates of the two images are, in clockwise order (starting top left) :
gradient: 62,129 421,218 383,458 147,548
tree: 445,100 765,47 698,368 529,396
I cannot figure out how to start with one image (in this case the sky background) and then take another image and draw that with perspective distortion to specific destination coords within the background. Also doing this with more than one image at a time, within one convert command, troubles me.
For example, when I start with just one image (the gradient) and try this:
convert sky.jpg \( gradient.png -alpha set -virtual-pixel transparent \
+distort Perspective "0,0 62,129 255,0 421,218 255,255 383,458 0,255 147,548" \) \
-compose src-over -composite result.jpg
It gets correctly warped (so the coordinates are relatively correct) but it's drawn in the top left corner, not at the coordinates I specify.
Also I'm a bit unsure if my usage of -compose and -composite is correct (I took this from various IM manual examples).
One other thing that is unclear to me: in case of the 256x256 image, should I use 255,0 and 255,255 and 0,255 as the corner coordinates, or 256,0 and 256,256 and 0,256 ?
Any IM experts who can shed light on these issues?
Add a -geometry just before the -composite like this:
convert -size 800x600 xc:black \( -size 300x200 xc:red \) -geometry +90+10 -composite result.png

imagemagick composite two png and keep it's alpha

I want to add watermark to my_own_png.png file by using:
composite -dissolve 10% -gravity center -quality 100 watermark_350.png my_own_png.png result.png
The result.png is not what I what, I want to keep my_own_png.png's alpha.
Please help~Thank you.
There are a couple of ways of doing this...
Firstly, you could run with your current processing, which I presume you understand and are happy with, and then copy back your original image's alpha over the top of your result, like this:
# Your current processing
composite -dissolve 10% ... result.png
# Now add back the original image's alpha
convert result.png my_own_png.png -compose CopyOpacity -composite result.png
Or, you could do the whole lot in one go using convert rather than composite, and it will look something like this but it is hard to be sure without seeing what you are actually doing:
convert my_own_png.png \
\( +clone -gravity center watermark_350.png -compose dissolve -define compose:args='10' -composite \) \
+swap -compose CopyOpacity -composite result.png
That basically loads up your my_own_png.png and puts it to one side. It then copies it and does the dissolve with your watermark inside the parentheses. After the parentheses, the order of the images is swapped so that the watermarked image is first in the list and the original png is second in the list. It then copies the alpha from the original image and applies it to the watermarked image - hopefully :-)

Export Multiple PSD Layer file to PNG while applying tint effect to each layer

We have the following PSD
https://app.box.com/s/rf514j3wnic1xkt6y1q3b5qnk0zds2rl
This is a transparent PSD with one base layer for the ring metal.
And additional layer for each individual stones.
Something like https://app.box.com/s/i8lhshbl27pvjmmczjq2bhla4mzw9cwn
and this
We want to be able to apply tint to each individual stone layer based of user input and export the final image as PNG file. To provide users the ability to design the ring to their liking.
Now I am trying to figure out what is the best way to achieve this functionality ?
We would like the image generated server side in .net so it can be shared.
What is the best way to approach this. I am thinking of using an image library like imagemagick to convert the image.
But I am unable to locate any examples where you can alter multiple layer with in a PSD file before converting it to another file format.
Any example or suggestion on methods to achieve this will of great help.
Editing layered PSD files is not going to be easy. I would suggest you maybe save the ring and each of the gemstone layers as a separate PNG file. Then you can do something along these lines with ImageMagick:
#!/bin/bash
convert ring.png \
\( layer-1.png \( +clone +level-colors red \) -compose Multiply -composite \) -compose overlay -composite \
\( layer-2.png \( +clone +level-colors green \) -compose VividLight -composite \) -compose overlay -composite \
\( layer-3.png \( +clone +level-colors blue \) -compose LinearBurn -composite \) -compose overlay -composite \
\( layer-4.png \( +clone +level-colors "#ffff00" \) -compose Saturate -composite \) -compose overlay -composite \
result.png
Giving you a PNG file like this:
You may like to experiment with the blending modes, I just tried a few that looked vaguely pleasing to my eye. If you want a list of all available blending modes, you can do:
identify -list compose
Atop
Blend
Blur
Bumpmap
ChangeMask
Clear
ColorBurn
ColorDodge
Colorize
CopyBlack
CopyBlue
CopyCyan
CopyGreen
Copy
CopyMagenta
CopyOpacity
CopyRed
CopyYellow
Darken
DarkenIntensity
DivideDst
DivideSrc
Dst
Difference
Displace
Dissolve
Distort
DstAtop
DstIn
DstOut
DstOver
Exclusion
HardLight
HardMix
Hue
In
Lighten
LightenIntensity
LinearBurn
LinearDodge
LinearLight
Luminize
Mathematics
MinusDst
MinusSrc
Modulate
ModulusAdd
ModulusSubtract
Multiply
None
Out
Overlay
Over
PegtopLight
PinLight
Plus
Replace
Saturate
Screen
SoftLight
Src
SrcAtop
SrcIn
SrcOut
SrcOver
VividLight
Xor
I also specified some colours by name and one by hex, so you can see how to do it that way if you prefer.
P.S. If you keep all your artwork as PSD files, you can always use Adobe's ExtendScript to script the export of the various layers as separate PNG files with a single keypress...
P.P.S. It is possible for ImageMagick to extract the layers from a PSD file itself, but I extracted the layers from your file and they are not all the same size as the background image and I cannot find their correct positioning relative to it. If you know something about how the layers were created and whether they could be made identical size and aligned with the background, you could replace layer-n.png in my examples with PhotoshopFile.psd[n]
P.P.P.S. If you want to do this server side in .NET you should take a look at the .NET library for ImageMagick that can be found here: https://magick.codeplex.com/. If you need help translating the commands above to C# you could ask a question on the discussions page there.

Compositing premultiplied images using ImageMagick

I have two images. One is background with no alpha. The other is a white cloud. The alpha of the cloud image is premultiplied with black. When I composite them the white cloud has black in it, so it looks grey instead of white like it should. I'm doing:
convert -gravity Center bg.tga whitecloud.tga -composite comp.tga
Is there a way to composite premultiplied images in ImageMagick, or does the image have to be non-premultiplied? Can I make a premultiplied image non-premultiplied using ImageMagick?
Update:
Ok, here are the images as TGA for download:
http://acatysmoof.com/posting/problems/imagemagick/premultiplication/bg.tga
http://acatysmoof.com/posting/problems/imagemagick/premultiplication/whitecloud.tga
http://acatysmoof.com/posting/problems/imagemagick/premultiplication/aftereffects.tga
http://acatysmoof.com/posting/problems/imagemagick/premultiplication/imagemagick.tga
and in the same order as jpgs to view in your browser:
I tried all the modes provided, but none of them create the same result as After Effects.
It would be easier if you showed your images, but try adding -compose lighten before -composite in your command, like this:
convert a.tga b.tga -compose lighten -composite out.tga
Basically that will make ImageMagick choose the lighter pixel of the two images at every point.
If that doesn't work, try other blending modes
for b in $(identify -list compose); do
convert -label "$b" bg.tga whitecloud.tga -compose $b -composite miff:-
done | montage - -tile 5x out.png
I am kind of thinking Atop, Dissolve, SrcAtop and SrcOver might be your friends but have a look full-size and see what floats your boat. That would be
convert a.tga b.tga -compose Atop -composite out.tga
Here is an Imagemagick command that does what you want:
convert -gravity Center whitecloud.tga -fx "u/max(u.a, 1/255)" bg.tga +swap -composite -fx "u*u.a" comp.tga
What's happening here?
-fx command #1: Convert whitecloud.tga from premultiplied alpha to "normal". The max() operator is a special case to avoid dividing by zero.
+swap command: Make bg.tga the first image and the revised whitecloud.tga the second.
-composite these two regular, non-premultiplied images.
-fx command #2: take the result, and return to a premultiplied alpha format.
This gives exactly the same result as After Effects.
Note that, as I wrote it, it only works for an opaque bg.tga. You'd need to do some extra work to handle a transparent background image.
If you want to duplicate the After Effects result, then I believe what you want to do in ImageMagick is the following -- composite the background image with a white image using the cloud as a mask:
convert bg.tga \( -clone 0 -fill white -colorize 100 \) whitecloud.tga -compose over -composite cloud_blue.tga
I have posted a JPG result, but my .tga result is the same.

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.

Resources