Add an image as a background to a previously manipulated image with Imagemagick - image

I'm writing a script that:
Scales
Crops
Adds a background image
to a series of around 400 transparent PNGs. I'd like to make this run as fast as possible.
Currently I've got it as two separate commands:
convert $input.png -scale $scale% -gravity Center -crop 640x640+0+0 +repage $output.png
composite -gravity center $output.png $background $output.png
Is there a way to reduce this to a single command? I've been looking at the docs for -composite but I can't see a way to add an image as the background after doing the other manipulation.
Any ideas? Thanks

Why don't you just use a && between the two commands? The second command will get executed when the furst exits without an error... Should work...

Related

Merge an animated gif over a static png at a specific location using ImageMagick

I have managed to paste an animated gif over a static png and obtained an animated gif from
those sources with the following command.
convert canvas.png animation.gif test.gif
and it works, but the animation and the canvas share the upper left corner, which is not what I want.
By reading a little I discovered that I have to use either the -composite option or the command, but I have not found examples of the right syntax to use.
I tried this:
convert canvas.png animation.gif -geometry +780+275 -composite test01.gif
which gives a static image with just the first frame of animation.gif
I tried other syntax but just produces errors.
What is the right way to accommodate the animation?
Try the following in Imagemagick. It takes a special null: operator to separate the two images and -layer composite. Also you must use -page or -set page rather than -geometry when using -layers composite. The following is Unix syntax. For Windows, remove the \ before the parentheses.
convert canvas.png null: \( animation.gif -coalese -set page +780+275 \) -layers composite -layers optimize test01.gif

Copyright/Watermark many images with different resolutions (with ImageMagick)

i am trying to solve the following problem:
I have one picture logo.png with a resolution of 1260x1580. I want to use this to copyright different JPG-Images, e.g. image1.jpg with a resolution of 1280x853. I want to have the logo e.g. at the bottom right and always with a height of e.g. 1/8 of the height of the JPG-Image.
Short: I have many images with different resolutions and the copyright should have always the same proportion (e.g. 1/8 of the total heigth) within that image.
I am using ImageMagick on command-line (on Windows 10), at the moment like this:
magick image1.jpg logo.png -resize x%[fx:u.h/8] -gravity SouthEast -geometry +15+15 -composite outfile.jpg
Unfortunately the resize-Option with x%[fx:u.h/8] doesn't work like expected, it also resizes the JPG-Image image1.jpg. I don't know, how i can manage it to resize only the logo.png and let the size of image1.jpg untouched. When i put ( logo.png -resize x%[fx:u.h/4] ) in brackets of course u.h then refers to logo.png, this is also wrong :-/
It would be great if you can assist me a little bit to find the right command for my purpose... Thanks for your replies and help!
PS: I hope that i didn't ignored some important source or help, but i can't find the solution by myself - sorry.
You have a stack of two images, the main input and the logo. You can have your "-resize" operation work on just the logo by adding a condition to the FX expression. Start your command with something like this...
magick image1.jpg logo.png -resize x%[fx:t==1?u[0].h/8:u[0].h] ...
The expression runs once for each image in the stack. The "t" substitutes for the position of the image in the stack, the first image is 0, the second is 1. So if "t" equals 1, it's the logo. Resize it to 1/8 the height of the first image indicated by "u[0].h/8". Otherwise resize it to "u[0].h", which is already the height of the first image, so it won't change that one.
In Imagemagick, try simply using parenthesis to limit the operation
magick image1.jpg ( logo.png -resize x%[fx:u.h/8] ) -gravity SouthEast -geometry +15+15 -composite outfile.jpg

MiniMagick: How do I add an overlay stock image that's larger than the source?

We're trying to achieve building a custom social sharing image from our app. The backend is built using Ruby/Sinatra and we already use MiniMagick+AWS-SDK to upload user images.
What we want to do is take the user's image and add a stock overlay (it contains a white circle as the placeholder for the variable user image) on top of the user image. I tried using MiniMagick's built-in composite method using Over as the operator, but the problem with Overis, if the overlay is larger than the source, it automatically adjusts according to the source's size. Our overlay is larger and we want to retain the overlay such that the resulting image has the complete overlay with the part of the user image that fits the placeholder white circle visible underneath.
I tried How to use Imagick to merge and mask images? but it doesn't solve my problem.
Basically, say this is the overlay.
And take this sample user image as example:
https://s-media-cache-ak0.pinimg.com/564x/71/08/b5/7108b5a89ce3d6cc0341f876681f8546.jpg
I would like the resulting image to be the complete overlay with the user image visible behind one of those while circles.
Update
As per Mark'sanswer, I wrote the following Ruby code to achieve the result:
avatar = avatar.resize "200x200"
result = mask.composite(avatar) do |c|
c.compose "DstOver"
c.geometry "+100+120" #this could be different depending on your mask's dimensions
end
You can test the result by executing result.write "result.jpg"
Not too sure what you are doing as your mask is an opaque JPEG? However, I think you mean to use a transparent PNG for the mask with a hole in it, in which case you could do this:
convert mask.png \( avatar.jpg -resize 200x200 \) -geometry +360+150 -compose dst-over -composite result.jpg

How to Join images of different size - adding Footer Image to all images (BULK)

I have more than 8000 images of different dimensions.
I want my footer image to adjust its width according to each image size and join the image at the bottom.
I am a basic person and will not be able to program it myself.
Is there any software that will help me do this?
You could use ImageMagick, which is installed on most Linux distros and available for OS X, all good OSes and Windows from here.
So, if you have an image like this:
the code to add a label would be pretty easy like this:
convert image.jpg -gravity center -background lime -fill yellow -stroke black -pointsize 36 label:"My Photo" -append result.jpg
I am not suggesting that is a good colour scheme, I am just showing you how/where to set the colours, and you can change them as you wish. If you now run the same code on a different size image like this:
you will get this:
There are countless examples here.
Please copy some of your images to a test folder and practice with the code below in there so you don't mess up your original photos. As I said, I don't really "do" Windows, but the code will look like this to do all the PNG and JPG files in a directory:
for %a in (*.PNG *.JPG) do convert "%a" -gravity center -background lime -fill yellow -stroke black -pointsize 36 label:"My Photo" -append "%a"
There are some good examples of using FOR loops here.
You can try Irfanview it has the paste special option that will resize and paste your footer image below the original one in one shot. But unfortunately it can't do it for you for the 8000 images.
To resize + merge a footer: Open your footer image, copy it (ctrl+c)
Now open your original image in IrfanView, go to Edit -> Paste Special -> to Bottom, it will resize & merge your footer under the original image.
IrfanView has also a batch conversion option to resize images in bulk but it can't join your images automatically. So it will help you with the resizing if needed.
To open the batch conversion in IrfanView, go to File -> Batch conversion/rename -> Advanced
From here you have tons of stuff to do among which the batch resize

Need a tool to set multiple image backgrounds to transparent

I'm searching for a software/tool to set the image background (black) to transparent.
I want to to remove all the black and make the image transparent.
All the images are .PNG and I have around 191 hence why I need a tool to do multiple images otherwise it could take me a very long time.
Use either graphicsmagick or imagemagick mogrify tool with the -transparent black option. When I ran it on your single sample image it looked like this:
gm mogrify -transparent black 'sprite sheet 4.png'
To do a few hundred of these, you'd use it like this:
for f in *.png ; do gm mogrify -transparent black "$f" ; done
Be sure to work on copies of the files -- mogrify modifies files "in place".

Resources