Apply gradient mask on image that already has transparency with ImageMagick? - imagemagick

I've got a PNG image with transparency:
original.png
Now I want to use ImageMagick to apply a diagonal gradient to its alpha channel. I mean so that its opacity remains in the top left corner, and gradually fades out to completely transparent in the bottom right corner. Like this:
result.png
So basically I want to generate a gradient, and use that as a mask for the image. But the image already has an alpha channel (transparency) of its own. Here's a visualisation of what I'm trying:
(original and result here displayed on checkerboard for visiblity, but I mean actual transparency)
I think I understand how to generate a diagonal gradient (the barycentric gradient command is very useful for this). But this creates a gradient in the color channels i.e. a colored or grayscale gradient. Whereas I want to apply the gradient on the alpha channel.
From the IM manual I understand the -compose CopyOpacity operator could be used for this. However this seems to copy the alpha from the mask on to my image. I need to "apply" this gradient color on my existing alpha channel, so basically I need my image's alpha channel to be multiplied by the grayscale color from the gradient image.
What would be the correct IM command line to perform the operation displayed above?

Here is one way you could do it:
convert tree.png -write MPR:orig -alpha extract \
\( +clone -colorspace gray -fx "1-j/h" \) \
-compose multiply -composite -write alpha.png \
MPR:orig +swap -compose copyopacity -composite result.png
The -write alpha.png can be omitted - it just shows the alpha layer for debugging and illustration purposes.
The MPR is just a temporary copy of the original image that I hold in memory while I am dinking around with the alpha channel and which I bring back near the end. The gradient in the alpha channel is generated by the -fx and I made the colorspace gray first so it only has to run once, instead of three times.
If you knew the dimensions of the tree image up front, you could replace the part in parentheses with:
-size WxH gradient:black-white
but I don't know the dimensions up front and I don't want a second convert command to get them, so I basically clone the original image's alpha channel to get a canvas the right size and fill it in with -fx.

Related

Wrapping an image over a product image convexly

What I want: When I apply an image over the pillow texture the artwork of the image should wrap convexly on the pillow.
Here is the link for the desired image
Above output is the correct output that I want
Note: In the above image, you can see the image is wrapped around the corners and convex from the center.
Problem: But when I apply image over the pillow texture image artwork is not curved convexly.
Here is the the output I currently have
Also, for reference,
here is the product image that I am using
And here is the artwork, that I am overlaying it with
Also, in the product image linked above, as you can see, there is some distortion in the top left corner, if I am overlaying it with any design, how can I achieve that distortion as well.
I am new to imagemagick, so are there any good resources to learn from and practice with?
Here is how to do that in ImageMagick.
Input:
First make a mask for the pillow. ImageMagick could not get a good mask, so I used http://remove.bg to make the background transparent and then extracted the alpha channel and got this:
I then measured the size and offset of the white part of the mask:
convert mask.png -format "%#" info:
642x646+78+77
Then I processed the images. I resized the text image, then applied pincushion distortion (opposite of barrel distortion) and inserted it into a white image the size of the pillow at the offset location from above. Then using the mask, I did a blend composite of the pillow and distorted image to make the result. See https://imagemagick.org/Usage/distorts/#barrel
convert pillow.jpg \
\( -size 800x800 xc:white \
\( pillow_overlay.jpg -resize 646x646 -distort barrel "0.0 0.0 -0.2 1.3" \) \
-geometry +78+77 -compose over -composite +geometry \) \
mask.png -define compose:args=60,40 -compose blend -composite \
pillow_new.jpg

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

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

Set alpha according to gradient with imagemagick

Is it possible to set the alpha channel of an image according to a gradient with ImageMagick?
I'd like the pixels on the left border of an image to be 100% transparent, and the ones on the right border to be 100% opaque, and with the ones in the middle having progressively lower transparency values.
Or in a more general case - given a grayscale image, set the alpha channel of another image as a function of the B&W values (black = 100% alpha, white 0% alpha).
With ImageMagick you can use -sparse-color to apply a gradient only to the alpha channel to get the result you describe.
convert in.png -alpha set -background none -channel A \
-sparse-color barycentric "0,0 none %[w],0 white" +channel out.png
That command starts by activating the alpha channel and setting the background color to transparent. Then it uses -channel A to apply the following operation only to the alpha channel. The -sparse-color operation tells it to start with transparent at the far left edge, pixel 0,0 and graduate to opaque at pixel %[w],0. The %[w] means the width or far right edge.
Although there are many ways to accomplish the effect you've described, by using -sparse-color you can easily make the gradient start and end at any positions on the image without having to create any intermediate masking images.
Simple. You would use -composite CopyOpacity to set the alpha channel from a gradient mask.
Given I have the following images. image.png & transparent_mask.png
We can set the image transparency (where black is alpha, and white is opaque) by copying values from the mask image to the input image alpha channel.
convert image.png transparent_mask.png -compose CopyOpacity -composite output.png

Extract watermark from image

I've got 2 versions of 1 image
Is it possible to get a transparent png file representing this watermark? Can ImageMagick do this?
SilverMonkey has the basic solution using Imagemagick. But the request was for a transparent PNG. So I will add a little bit more to his code to make it transparent by adding -alpha copy.
convert kitty2.jpg kitty1.jpg -compose minus -composite -auto-level -alpha copy watermark1.png
Here is another approach that makes a binary mask for the watermark by thresholding. But it leaves a lot of noise. So I use some morphology open to remove the noise and then some morpholgy close to try to fill in where the text is broken up. Then I add -alpha copy to make the image transparent. But the text is white and the original watermark was light gray. So I turn alpha off, multiply by 0.75 to reduce the brightness of the white letters to gray without affecting the alpha channel. Then turn the alpha channel back on.
convert kitty2.jpg kitty1.jpg -compose minus -composite -threshold 0.6% -morphology open diamond:1 -morphology close octagon:1 -alpha copy -alpha off -evaluate multiply 0.75 -alpha on watermark2.png
For more on morphology, see https://imagemagick.org/Usage/morphology/
You can achieve your goal by calculating the difference between both images (subtract the pixels of both images and calculate the absolute value). This will result in:
ImageMagick seems to be capable of image subtraction, look here:
The code:
convert image2 image1 -compose minus -composite result

Applying a circle gradient with imagemagick

I want to draw a circular gradient mask on an image, so that the image starts to fade at the outside of the circle with radius R, and fades to white with radius r < R. Is there a way to do this + how?
Example source image: (not the real one I'm trying to work with, but it doesn't matter)
If I do
convert flag1.png -fill white -draw "circle 400,234 200,200" flag2.png
then I get this:
In gimp I an do something like this with a radial gradient in another layer; my attempt looks crappy but this is vaguely what I want, except that it's not a smooth transition at the circle boundary, and my poor hand-eye coordination means I didn't get it centered:
Hmm: I muddled my way (via http://www.imagemagick.org/discourse-server/viewtopic.php?t=16692 ) to
convert flag1.png -size 800x468 radial-gradient:black-white -compose copy_opacity -composite flag3.png
which gives me something that's close to what I want with r=0 (never really completely fades out except at the very center); is there a way to expand to a white hole that fades out to a larger hole? And what does -compose copy_opacity -composite actually do?
Not sure what you are looking for exactly, but you could experiment with the percentages in this:
convert flag.png \( -size 800x468 radial-gradient:black-white -contrast-stretch 60%x0% \) -compose copy_opacity -composite flag3.png
The part in parentheses makes a black and white circle, and the -compose copy_opacity -composite part adopts that black and white circle as the alpha/transparency layer of the original flag image.
The parentheses ensure that the contrast stretch is only applied to the alpha/transparency layer and not the original coloured image.

Resources