Imagemagick: how to convert images with different size to image 1920x1080 (white background) - image-processing

I've a folder with several images with different size (i.e. 1284x456, 1290x445 and so on...).
I need to convert each image to 1920x1080px:
enlarging the sizes proportionally so that I have them 1920px large (i.e. 1920x681, 1920x662 and so on...);
and then adding a white background so that the final height of the output image becomes exactly 1080px (with the old image vertically centered).
Is it possible with a single command?
thanks

I think that I found the trick:
for i in *.jpg; do convert -resize 1920x1080 -gravity center -background white -extent 1920x1080 $i `basename $i `.JPG; done

Related

How to merge 2 images (border, actual image) with different sizes into 1 in linux

I have around 2500 images. Some of them contains border while others arent. What I want to accomplish is to add border to the ones that doesn't have any.
Images with border are 64x64 and images without border are 56x56
I prepared a border image file with size of 64x64 in PNG format and I want to merge all files without border within this image to get them borders and output them as PNG format aswell.
I have tried several ways using ImageMagic but no luck so far.
If you make a 64x64 lime-magenta gradient as background.png and a solid blue 56x56 image as foreground.png like this:
magick -size 64x64 gradient:lime-magenta background.png
magick -size 56x56 xc:blue foreground.png
Your command to paste the foreground into the centre of the background is as simple as:
magick -gravity center background.png foreground.png -composite result.png
I figured out it's pretty doable with convert tool from the ImageMagick package.
First I extracted all images with size of 56x56 to different location, then I did the following;
I added 4 pixels on each side since my border is 64x64
for image in ICO_*.png; do
convert -page +0+0 border.png \
-page +4+4 $image \
-background none -layers merge +repage $image-withBorder.png
done

crop / swap parts of image with magick mogrify

I am trying to crop and swap different parts of a big 800x800 image and re-create 800x800 image using imagemagick with this command.
magick mogrify titli.gif -crop 2x4# +repage -reverse -append -path converted titli.gif
my problem is "-append" creates tall image (400x1600) & "+append" creates wide image (3200x200)
How can I get a large image of original size 800x800 but with cropped and swapped (reversed) parts set in "mosaic or tiled" style...
If I understand the question, you shouldn't need "mogrify" to do that. Just "magick" should accomplish that task.
It looks like you'll have to crop the image into 8 pieces, reverse them, and "-append" them vertically as you've done.
Then after that, and in the same command, you'll need to crop that result in half vertically and "+append" those two pieces horizontally to get the 800x800 output.
This example command shows how it works...
magick in.png -crop 2x4# -reverse -append -crop 1x2# +append out.png
If you're doing any more operations within the same command you'll probably want to use "+repage" after the "+append" to reset the image geometry back to WxH+0+0.

Convert image from 4500x5400 to 4050x4200 keeping aspect ratio

I have an image of 4500x5400, I want to resize it for the height 4200 keeping the width ratio, however the width of the image needs to be 4050, leaving the sides transparent.
I have this ImageMagick command:
convert file.png -resize 4500x5400 -gravity center -background transparent -extent 4050x4200 out.png
However it's cutting the top and the bottom, while it needs to appear.
Do you guys have any idea of how I can make it work?
Appreciate your time!
Try this...
convert input.png -resize 4050x4200 -background none -gravity center -extent 4050x4200 output.png
The "-resize" fits your input image within a container of that size. The "-extent" makes sure the total canvas is that size. The "-background" and "-gravity" make sure extra space is filled with transparent and that the input image is located in the center of the output canvas.

ImageMagick: resize with borders in batch

I have a few images I want to convert in batch using ImageMagick.
All images should be converted to a specific width, lets say 500px.
If an image's width is larger than 500px, scale it down and preserve the aspect ratio.
If an image's width is less than 500px, add borders to the sides to make it 500px wide.
The first one is possible using magick mogrify -resize 500x *, but this also scales images with a smaller width up. How can I prevent that from happening and add borders instead?
I figured I could just add -background black -gravity center but I don't know how to prevent upscaling.
Solved it using two separate commands for now:
Resize images larger than 500px wide:
magick mogrify -resize '500x>' *
Add borders to images smaller than 500px wide:
magick mogrify -gravity center -extent '735x<' *

How can I overlay a larger watermark/frame image on a smaller one?

I have an overlay image, which is like a watermark/logo, which needs to be overlayed on top of the source image (while preserving alpha channel, etc)
When overlay is the same or smaller dimension as the source image - things are easy:
composite.exe -alpha on -gravity center logo.png in_image.jpg out_image.jpg
However, when logo.png is larger than in_image.jpg - above call truncates the logo, and out_image.jpg has the same dimensions as in_image.jpg
I would like the resulting image to be the largest of either the logo.png or in_image.jpg so I can do things like artistic frames around the photos.
Below image demonstrates the end result I want to be able to get this:
Desired Result
Note, here, the png with skulls has larger dims than the kiddo's image. The alpha channel needs to be preserved.
Edit: more clarity through examples
Here is another desired result
Here, the png file is opaque on the sides, has a clear window in the middle, and half-translucent bubbles. the JPG file is just a regular JPG from a camera.
Would love to add original and logo files that result in it, but lack reputation to add more than 2 links (or to add images)
Updated Answer
If you have v7 of ImageMagick, you can get it to do the maths for you all in one line using -fx to determine the dimensions of the larger of the two images:
magick background.jpg overlay.png -background none -gravity center -extent '%[fx:u.w>v.w?u.w:v.w]x%[fx:u.h>v.h?u.h:v.h]' -composite result.png
That basically says... "Extend the two images as follows. If the width of the first image (u.w) is greater than that of the second image (v.w), then use the width of the first, else use that of the second. Same for height.".
Original Answer
I believe you want this. Get width of whatever is wider of background and overlay. Get height of whatever is taller of background and overlay. Extend both background and overlay with transparent pixels to new dimensions. Overlay.
So, if we start with this as background (300x50):
And this as overlay (122x242) - which is a tall blue rectangle surrounded with transparency then that is bordered in black to show the extent of it:
You would run this, which is actually very simple but it is full of comments and debug output so you can see what is going on:
#!/bin/bash
# Get background width and height
read wb hb < <(convert background.jpg -format "%w %h" info: )
echo "Background: " $wb $hb
# Get overlay width and height
read wo ho < <(convert overlay.png -format "%w %h" info: )
echo "Overlay: " $wo $ho
# Get wider of the two
w=$wb
[ $wo -gt $w ] && w=$wo
# Get taller of the two
h=$hb
[ $ho -gt $h ] && h=$ho
echo "New dimensions: " $w $h
convert background.jpg overlay.png -background none -gravity center -extent ${w}x${h} -composite result.png
Here is how it looks running:
Background: 300 50
Overlay: 122 242
New dimensions: 300 242
Presumably, when you have done your overlay, you would add -trim as the final part of the command line to remove any extraneous stuff that has been added.
Try using convert rather than composite. It is more flexible than composite.
convert logo.png in_image.jpg -gravity center -compose over -composite out_image.jpg
But if you insist on using composite, then
composite in_image.jpg logo.png -gravity center -compose src_over out_image.jpg
Note sure exactly what you mean by preserving the alpha channel, since jpg does not support transparency. Perhaps you can post a pair of inputs and your desired output, if what I have posted does not do what you want.

Resources