Batch Resize Photos While Maintaining the Aspect Ratio AND Fitting to a specific pixel size - image

I am looking to batch resize pictures to a specific pixel size while maintaining the aspect ratio. Is their a way that the picture aspect can be preserved and the rest of the space can be filled in with white? For example, I resize an image to 200 x 200 and due to preserving the aspect ratio it is changed to 200 x 194. I would like the actual image to remain 200 x 194 and white space to fill in the remaining area to create an image that is 200 x 200 px. Thank you in advance!

You can do that with ImageMagick which is included in most Linux distros and is available for macOS and Windows.
Just in Terminal, or Command Prompt on Windows:
magick input.jpg -background white -resize 200x200 -gravity center -extent 200x200 result.jpg
If you have many files to do, you may be better using ImageMagick's mogrify command, which will do them all in one go for you! So make a new directory called processed for the output files and then use this to process all PNG files in the current directory:
magick mogrify -path processed -background white -resize 200x200 -gravity center -extent 200x200 '*.png'
I don't know if you are on Windows or not, so you may or may not need the single quotes around the filenames at the end of that command. Basically, it determines whether the expansion of the filename list is done by the shell (which has limitations on the number of files), or internally by ImageMagick (which does not).
If you are running anything older than v7, the commands become:
convert input.jpg ...
or
mogrify ...

Related

Batch convert white background to transparent

Seeking to set a folder of PNG line drawings to background transparent and getting lost in examples and options recommended for other cases. Essentially when I find a line of command that seems to fit I don't know how to apply it to an entire folder. Also getting lost in options like fuzz and anti-alias which I'm not even sure would benefit my images, below. I'm on a Mac running v7 of Magick and really appreciate your kind help.
Simplest Sample
Most Complex Sample
This should do what you want in Imagemagick 7. You should use magick mogrify to process a whole folder of images. To convert white to transparent in an image you can use -fuzz XX% -transparent white, where XX is a percent tolerance for nearby colors. For JPG, typically about 15% seems to work due to jpg compression change of flat colors. So I put your images into a folder called drawings and create a new empty folder called drawings2 to hold the output image, which need to be PNG (or TIFF) so use transparency.
cd
cd desktop/drawings
magick mogrify -path ../drawings2 -format png -fuzz 15% -transparent white *.jpg
Results:
See https://imagemagick.org/Usage/basics/#mogrify

Imagemagick input image to thumbnail squared image

As stated in the title i have to create a squared image starting from any kind of input image.
If the input image is smaller than thumbnail then i have only to pad it, otherwhise (if geater) should be shrinked without change aspect ratio) so for example :
After few test come up with the following code :
mogrify -auto-orient -define jpeg:fancy-upsampling=off:size={with}x{height}^> -thumbnail {with}x{height}^> -gravity center -extent {with}x{height}^> -background white -quality {dpi} -format {format} -path {path} *.*
Everything work as intended, except for few corner cases.
If the input image is really smaller compared to thumnail size, then the input image tend to disappear (because really small compared to thumbnail size).
So i am trying to figure it how to avoid excessive padding (only the strictly necessary to make the picture squared) when source image is smaller than thumbnail size (and so due to '>' command imamagick will not shrink it).

AutoMerge two images files to one

I am using Montage command-line tool to merge the two jpg images. The output jpg contains the common strip present in the input jpgs. Below is the command to merge two jpgs:
montage -geometry 500 input1.jpg input2.jpg output.jpg
How can I avoid the common area in the output file?
Is there any other tool available to auto-merge the two images?
In ImageMagick, you can simply append the two images side by side or top/bottom.
convert image1.jpg image2.jpg -append result.jpg
will do top/bottom
convert image1.jpg image2.jpg +append result.jpg
will do left/right.
You can append as many images as you want of different sizes. You can use the -gravity setting to align them as desired. If different sizes, then you will have background regions, which you can control the color by using -background somecolor. If desired, you can resize the images by adding -resize 500 after reading the inputs and before the append.
See http://www.imagemagick.org/Usage/layers/#append
I suspect you are trying to make a panoramic by stitching two images with an area of common overlap.
So, if we start with left.png:
and right.png:
You probably want this:
convert left.png -page +200 right.png -mosaic result.png
Just so you can see what happens if I change the x-offset and also how to add a y-offset:
convert left.png -page +280+30 right.png -mosaic result.png
If you want to do what Mark Setchell is suggesting, then using -page is probably the best method, if you have more than one image to merge and the offsets are different. If you only have on pair of image, you can overlap them using +smush in ImageMagick. It is like +append, but allows either overlap or a gap according to the sign of the argument. Unlike -page, it only shifts in one direction according to +/- smush. Using Mark's images,
convert left.jpg right.jpg +smush -400 result.jpg

Using ImageMagick to add whitespace to images in bulk

I have a bunch of images that are rectangular and I need them to be square, i.e. 1000x1000, so I've decided to use ImageMagick to pad the sides of the image with the amount of whitespace necessary to make the image square.
I've found the code from the following link useful (see next paragraph). But this requires me to process each image individually, to set the filename and then the -thumbnail dimensions. How can I use this same process to process an entire folder?
Here's the code and the link to it:
http://imagemagick.org/Usage/thumbnails/#pad
convert -define jpeg:size=200x200 hatching_orig.jpg -thumbnail '100x100>' \
-background white -gravity center -extent 100x100 pad_extent.gif
It is not very clear from your question what you actually have (i.e. how big your images are? what format are they?), nor what you actually want (you only provide an example that doesn't do what you want. But I'll take your question at the face-vale of the title and hope you can work out anything missing from there.
If you want to do ImageMagick in bulk, you either need to use a loop in bash, like this:
#!/bin/bash
for f in *.jpg; do
convert "%f" ...
done
or use IM's mogrify command. That is quite a powerful command, so make a backup of your images before you even think of trying to use it.
Let's assume you want to place all JPEG images in the current directory onto a white 1000x1000 pixel background. You would do this:
mogrify -background white -gravity center -extent 1000x1000 *.jpg
Updated Answer
It should be possible to do the resizing and the white padding in a single ImageMagick command without needing to use SIPS as well, like this if you now want 1200x1200 square images padded with white:
mogrify -background white -gravity center -resize 1200x1200\> -extent 1200x1200 *.jpg

ImageMagick: Can I control which instance is used from a multi-resolution .ico file?

We have an in-house tool that does some operations on windows icon files. Turns out that it only really supports the type of .ico files that are generated by ImageMagick. Should be no big deal because we currently use ImageMagick anyway to generate sizes not supplied in the original image. My plan was to always use ImageMagick to generate images, even when the original .ico file contained an image with the correct resolution, to ensure the image was of the correct format.
So far, so good. However in practice this does not seem to work the way I expect it to. Specifically I'm after a 256x256 icon. The original file contains (I believe) a 256x256 version as it looks high res when viewed from the browser. However, the output file from ImageMagick is obviously a low-resolution one scaled up.
The command line I'm using is:
convert.exe orig.ico -background #FFFFFFFFFFFF0101 -compose Copy \
-gravity Center -scale 256x256! -depth 8 temp2.ico
Question: is there some better set of commands to use, which might start from the 256x256 icon?
Your orig.ico file probably contains multiple dimensions. They will also be stored in the output file. This means a dimension of 16x16 will be upscaled to 256x256 and that produces an ugly image. The 256x256 resolution is most likely the first dimension. You can see all the dimensions with the following command:
identify.exe orig.ico
With the command below you can get a specific dimension/image by specifying the index (starts at zero):
convert.exe orig.ico[0] -background #FFFFFFFFFFFF0101 -compose Copy ^
-gravity Center -scale 256x256! -depth 8 temp2.ico
If the 256x256 dimension is the last image you should set the index to -1:
convert.exe orig.ico[-1] -background #FFFFFFFFFFFF0101 -compose Copy ^
-gravity Center -scale 256x256! -depth 8 temp2.ico

Resources