Selectively rotate vertical images - imagemagick

I have a directory of images, some of which are vertical (height x width) 7 x 5 while others are horizontal 5 x 7. I'd like to use imagemagick (or other tool) to rotate only the verical images to horizontal. Is this possible to do with imagemagick?

As per http://www.imagemagick.org/script/command-line-options.php#rotate, you can conditionally rotate images:
convert input.jpg -rotate 90< output.jpg
This will only rotate, clockwise by 90 degrees, if the height exceeds the width (what you described as a "vertical" image, widely known as portrait orientation).

Related

ImageMagick Montage: Setting image size and page size

I am trying to use montage to print proxies for a card game I am developing (similar to Magic: the Gathering). I want the set the page size to letter, display four images per page, and have the images at 2.5 x 3.5 inches at 300 DPI.
I use the -page letter argument to get the right page size. I set -tile 2x2 so that I have four images per page. I set geometry +10+10 to get some space between the images.
However, montage resizes the images and they each take about 1/4 of a page, which is much bigger than what I want. I tried setting the pixels on geometry, for example, -geometry 744x1039+10+10 -density 300 which is 2.5 x 3.5" in 300 DPI, however, that did not work -- the image sizes remain the same.
How can I set the page DPI to 300 and the size of each image in the montage to 2.5" x 3.5"?

Resize to Inches Using ImageMagick Convert?

I'm using the following command to resize all the images in a folder:
convert folder\*.png" -format jpg -resize 1573 -quality 70 -strip -density 72 -interlace Plane -set filename:fname %t-1 +adjoin "C:\Users\%USERNAME%\Desktop\New folder\%[filename:fname].jpg"
It works, but instead of resizing to 1,573 pixels, I'd like to resize to 8.5 inches in width and 11 inches in height. How can I do that using ImageMagick Convert?
The "print size" of an image is the combination of its size in pixels and it print resolution, the matter being just metadata:
print size (inches) = size in pixels ÷ print resolution (in PPI)
So to change your image to 8.5×11 inches you can just change the resolution:
Necessary resolution = 1513 pixels ÷ 11 inches = 138PPI
The usual tool to edit metadata is ExifTool:
exiftool -xresolution=138 -yresolution=138 -v2 your_image.jpg
This of course assumes that your image is already in the 11 ÷ 8.5 aspect ratio. Otherwise you compute different X and Y print resolutions or first crop the image to achieve the proper aspect ratio.
Also, watch out for low resolution values. If you have text or sharp lines in the image (logo, CGI) 150PPI is on the low side...

Cropping a variable width frame with graphicsmagick

I have some images that have a transparent border of varying width; I'd like to crop these to the visible parts of image. (I imagine a solution won't be specific to the transparent part, but would work with a frame of any color.)
E.g. the 200x200 image below has a transparent border of 3 pixels. I'd like to get the equivalent of
$ gm convert a.png -crop 194x194+3+3 b.png
without having to determine the numbers by hand. Is this possible with graphicsmagick (or imagemagick)?

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<' *

In ImageMagick, how can I scale an image down just enough so it's cropped to particular dimensions?

For example, if I have a large image that's 1600x1200, but I want to generate a thumbnail that is 250x200 (a different ratio of height and width) -- how can I scale this down to size and then center crop it? Is this possible in one line using convert?
Scale down + fill given area (and stretch output image if required)
If you want to scale down to 250x200 pixels (without keeping the aspect ratio), completely filling the space of 250x200 (stretching/distorting the image if necessary), use:
convert \
input.jpeg \
-scale 250x200\! \
output1.png
(Note the \! characters after the scale geometry setting!)
Replace the .jpeg and .png suffixes with whatever format suffixes you have and want, ImageMagick will automatically handle it.
Scale down + fit result into give area (and keep aspect ratio, and if required, waive some of the area given)
If you want to scale down to inside 250x200 pixels while keeping aspect ratio (the ratio of height to width) of the original image and cropping that dimension of the 250x200 pixels which isn't used up, use:
convert \
input.jpeg \
-scale 250x200 \
output2.png
By default width and height given in the -resize argument are maximum values (unless you specify it as a percentage). This means: output images expand or contract to fit the specified dimensions, maintaining the aspect ratio of the image.
This command above "tries" to set the dimensions to 250x200:
When the height is shrunk to 200 pixels from the original's 1200 pixels (to 16.6666% of this original value), the width is now 266.66 pixels. So it doesn't fit yet....
When the width has shrunk to 250 pixels from the original 1600 pixels (to 15.625% of this original value) the height now is 187.5 pixels. So it fits now, and can leave over some space at the top and bottom.
So the final dimension will be either of 250x187 or more likely, 250x188 pixels (you can't have fractions of a pixel in the height, it requires an integer!).
Scale down + center result in given area (and use some background color to fill remaining space)
If you want to scale down to inside 250x200 pixels, keeping aspect ratio of the original and creating an image that is the full-sized 250x200 pixels centering the real image in that space by adding some yellow background, use:
convert \
input.jpeg \
-scale 250x200 \
-gravity center \
-background yellow \
-extent 250x200 \
output3.png
Replace the background color with any other you like better, or with none for transparent background (not supported for JPEG output!).
Scale down keeping aspect ratio + without cropping to given area
If you want to scale to 250x200 pixels keeping the aspect ratio, completely filling the space of 250x200 (but not cropping off any overflowing), use:
convert \
input.jpeg \
-scale '250x200^' \
output4.png
(Note the ^ character after the scale geometry setting. The ^ feature requires a minimum ImageMagick version of 6.3.8-2.)
When modifying width and height with the ^ addition to the -scale or -resize arguments, these settings are considered to be minimum values where only one of these too dimensions is really attained. This means: output images expand or contract until either width or height matches its specified dimension, maintaining the aspect ratio of the image.
This command above "tries" to set the dimensions to 250x200:
When the height is shrunk to 200 pixels from the original's 1200 pixels (to 16.6666% of this original value), the wanted scaling is achieved. Width is now 266.66 pixels. This is rounded to 267 pixels.
So the final dimension will be 267x200 pixels.
Scale down keeping aspect ratio + with cropping to given area
If you want to scale to 250x200 pixels keeping the aspect ratio, completely filling the space of 250x200 (cropping off strips from left and right edges), use:
convert \
input.jpeg \
-scale '250x200^' \
-gravity center \
-extent 250x250 \
output5.png
The final dimension of the image will be 250x200. Compared to the previous command (which gave a 250x267 result), some pixel columns from the left and right edges are removed.
Accepted answer doesn't do what question asks.
If you need exact output dimension (for example 250x200), fit maximum information from the original image ang crop redundant parts to keep aspect ratio, use this command:
convert input.jpg -scale "250x200^" -gravity center -crop 250x200+0+0 output.jpg

Resources