how to make this effect with imagemagick - imagemagick

The backgroud pic:
background
The final effect:
gif effect
How do I make the red heart move and cover the background text?
I have tried this:
Convert back.png ( -clone1 -gravity center -geometry +10+10 heart.png ) (-clone 1 *******)
This just moves the heart, but did not cover the text.
How do I make the red heart move and mask part of the background text?
With mask?

Here is one way using Imagemagick. First I need to crop out the heart shape from one of your animation frames, since you did not provide that.
Heart Image:
Background Image:
First, I create a new folder on my desktop called test and put both images into it.
Here is the code in (Bash) Unix syntax.
I start backwards from the right side towards the left of the background. The i indices are then reversed using j=(55-i) for later combining in alphabetic order. The jj computation adds a leading zero to the indices less than 10 so that all frames are listed in the test folder in alphabetic order.
For each frame, it extracts the alpha channel of the background and draws a black rectangle on the right side and then puts that result back into the alpha channel of the background image using -compose copy_opacity -composite. This effectively puts transparency where the pink letters are. Then I composite the heart image as the corresponding left edge of where I had drawn the black rectangle.
After the loop finishes, I gather all the frames from the test directory with names starting with tmp_ and create a gif animation that I write to the desktop.
x=564
for ((i=0; i<56; i++)); do
j=$((55-i))
jj=`printf "%02d\n" $j`
convert background.png \
\( -clone 0 -alpha extract -fill black -draw "rectangle $x,0 639,123" \) \
-alpha off -compose copy_opacity -composite \
heartshape.png -geometry +${x}+28 -compose over -composite tmp_$jj.gif
x=$((x-8))
done
convert -dispose background -delay 10 tmp_*.gif -loop 0 ../heart_animation.gif
Note that Windows Imagemagick and scripting syntax is different. If some kind Windows user wants to post the conversion, that would be great.

Related

ImageMagick get image side only - not a edge detection problem

This is not a finding edge problem but I am trying to find the background color of the image, in order to find it, I am trying to get the edge of the image and guess the best color.
In order to do so, I want to remove the center of the image and just get image like a frame with transparent center.
Is it possible through ImageMagick?
For example
If want
Try this tested on Windows with IM V7:
magick "input.png" ( -size 200x200 xc:black ) -gravity center -compose DstOut -composite "output.png"
Takes the original photo, creates a rectangular mask, places it over the middle of the image and "cuts" out the rectangle
If the input is not a png ( for instance a jpg ) you will have to do some work setting the alpha channel
EDIT: This seems to work for both jpg and png images
magick "input.png" -alpha on ( -size 200x200 xc:black ) -gravity center -compose DstOut -composite "output.png"
I'll offer a simple variation on Bonzo's solution. This command allows you to specify in pixels the amount of edge you want to keep...
magick input.jpg -alpha on -bordercolor none -background none ^
( +clone -shave 24 -border 24 ) -compose dstout -composite output.png
It reads the input image, sets the alpha "on", and sets the border and background colors to "none".
Then inside the parentheses it makes a clone of the input image, shaves off the amount you want to retain around the edges, then adds that amount back as a transparent border all around. That will be the mask.
Then after the parentheses use a compose method of "dstout" to composite that mask onto the input image, which results in a transparent "hole" in the image with your specified amount of the original edge retained.
If you want to keep different amounts from the vertical and horizontal edges, specify those amounts in the shave and border operations...
... -shave 24x32 -border 24x32 ...
If you're using IMv7 you can even use FX expressions to specify the widths...
... -set option:v1 %[fx:(w+h)/2*0.1] -shave %[v1] -border %[v1] ...
The command is in Windows syntax using IMv7. Change "magick" to "convert" to use it with IMv6. To make it work in *nix you need to change the continued-line caret "^" to a backslash "\" and escape the parentheses with backslashes "\(...\)".

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

Image rotation relative to wrong (top right) point

I am trying to place a rotated image onto a blank canvas.
This is the command i am using:
convert "(" -size 1000x1000 xc:transparent ")" "(" "img.jpg" -virtual-pixel white -rotate -10.75 ")" -geometry +136+148 -composite "overlay.png" -geometry +0+0 -composite out.png
I would expect the image to appear at +136+148 rotated around its center. However, it appears to be rotated around its upper right corner instead.
I confirmed this in Photoshop:
This is the resulting image (using slightly different size but same parameters as my example command given here):
The 4 lines are the guides that mark the coordinates that define where the image sits. As you can see, the image is rotated -10.75 degrees. And the rotation center is the upper right corner.
Here i select the box at the correct coordinates in Photoshop:
And here i rotate it after setting the rotation point to the top right:
As you can see, the selection now matches the image as it was calculated by my convert command.
I have tried dealing with this for many hours. I tried switching to +distort and -distort and what not, but there i simply get either a cropped image or another wrongly positioned bounding box.
I don't know how to address this problem. What am i doing wrong? How can i tell -rotate to rotate my image around its center, not around the top right corner (why top right anyway?).
Thank you for your help!
The default gravity for offsetting is the northwest corner or top-left corner. The geometry offset you are using moves the top left corner of the padded rotated image after its background has been added and not the top left corner of the where the Input gets rotated. So I think that may be why you are not getting the expected result. ImageMagick -rotate does rotate about its center, but it fills the background out to make a complete rectangular image that is the bounding rectangle about the rotated image.
In ImageMagick, a better approach to what you did would be to use -gravity center and align the center of the rotated input image with the center of the transparent region in the overlay image.
Alternately, measure the 4 corners of your transparent region in the overlay and 4 corners of the input image and do a perspective or affine distortion to align the two.
Here is how to do it in bash unix syntax.
over1="122,186"
over2="466,118"
over3="510,345"
over4="166,411"
WxH=`convert -ping "Image.jpg" -format "%wx%h" info:`
ww=`echo "$WxH" | cut -dx -f1`
hh=`echo "$WxH" | cut -dx -f2`
ww=$((ww-1))
hh=$((hh-1))
in1="0,0"
in2="$ww,0"
in3="$ww,$hh"
in4="0,$hh"
convert overlay.png \
\( Image.jpg -virtual-pixel none +distort Perspective \
"$in1 $over1 $in2 $over2 $in3 $over3 $in4 $over4" \) \
-layers merge +repage \
out.png
See https://imagemagick.org/Usage/distorts/#perspective
Try using the offset geometry that aligns with the outer edges of your target area. That would be more along the red lines I've added here, not along the cyan lines in your example image.
Edited to add: For a method of finding the proper offset information to fill the hole and placing the image under the hole, see my other answer in this thread.
You can find the location of the cutout in your overlay image, then use that information to properly place your under image with a command like this...
convert overlay.png -background none \
\( +clone -alpha extract -trim \
-set option:offset +%[fx:page.x]+%[fx:page.y] +delete \) \
\( image.jpg -rotate -10.7 -set page %[offset] \) \
-set page %[fx:u.w]x%[fx:u.h] +swap -layers merge result.png
This makes use of the fact that the bounding box dimensions of the rotated input image are the same as the bounding box dimensions of the cutout of the transparent region in the overlay image.
That reads in the overlay, and inside parentheses it makes a clone of it, extracts the alpha channel, and trims the result to find the offset of the cutout. It saves the offset to a variable named "offset".
Then inside another set of parentheses it reads in the under image, rotates it the known -10.7 degrees, and uses that variable "offset" to set the paging offset on the rotated "image.jpg".
Then outside the parentheses it uses the width and height of the overlay to set the paging dimensions for the under image.
It finishes by swapping the order of the images to put the overlay on top, then merging the two layers to create the output.
Here is an alternate to GeeMack's excellent ImageMagick solution. This computes the center of the original input.jpg, and the center of the transparent region in the Overlay.png. Then it uses +distort SRT to translate and rotate the image before finally compositing with -layers merge.
convert Overlay.png \
\( +clone -alpha extract -trim -set option:center "%[fx:page.x+w/2],%[fx:page.y+h/2]" +delete \) \
\( Image.jpg -virtual-pixel none +distort SRT "%[fx:w/2],%[fx:h/2] 1 -10.7 %[center]" \) \
+swap -layers merge +repage result2.png
The second line is much line GeeMack's, but it computes the center of the transparent region's bounding box and saves that in the "center" argument and then deletes the trimmed image.
The third line computes the center of the Input.jpg image (before rotation). It then uses +distort SRT to translate from the input center, (scale=1) and rotate -10.7 and translate to the center of the transparent region's bounding box.
See https://imagemagick.org/Usage/distorts/#srt

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.

Apply gradient mask on image that already has transparency with 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.

Resources