As pointed out by LifeHacker, the following command will do this very easily:
sips -Z 640 *.jpg
To quote their explanation:
"sips is the command being used and -Z tells it to maintain the image's aspect ratio. "640" is the maximum height and width to be used and "*.jpg" instructs your computer to downsize every image ending in .jpg. It's really simple and shrinks your images very quickly. Be sure to make a copy first if you want to preserve their larger size as well."
Here is script that uses sips to recursively resize all the images in a given folder (and its sub-folders), and places the resized images in a resized folder on the same tree level as the image: https://gist.github.com/lopespm/893f323a04fcc59466d7
#!/bin/bash
# This script resizes all the images it finds in a folder (and its subfolders) and resizes them
# The resized image is placed in the /resized folder which will reside in the same directory as the image
#
# Usage: > ./batch_resize.sh
initial_folder="/your/images/folder" # You can use "." to target the folder in which you are running the script for example
resized_folder_name="resized"
all_images=$(find -E $initial_folder -iregex ".*\.(jpg|gif|png|jpeg)")
while read -r image_full_path; do
filename=$(basename "$image_full_path");
source_folder=$(dirname "$image_full_path");
destination_folder=$source_folder"/"$resized_folder_name"/";
destination_full_path=$destination_folder$filename;
if [ ! -z "$image_full_path" -a "$image_full_path" != " " ] &&
# Do not resize images inside a folder that was already resized
[ "$(basename "$source_folder")" != "$resized_folder_name" ]; then
mkdir "$destination_folder";
sips -Z 700 "$image_full_path" --out "$destination_full_path";
fi
done <<< "$all_images"
Previous answers are correct, you can use mogrify too. For example, if you want to reduce the size of many images in a directory by 60% then you can use the command below:
of course always make a backup of your images in to another directory before playing with this command.
Many people here have mentioned imagick, but it's not enough (and fast) for me, especially when I want to reduce only the image whose width/height is larger than a dimention, and leave all others smaller.
An example of resizing all images in the current folder (only for images with dimension(s) larger than 1280x1080) and outputting them to an existing "out" folder,
General description (actual behavior can vary for different options and settings)
scale%
Height and width both scaled by specified percentage.
scale-x%xscale-y%
Height and width individually scaled by specified percentages. (Only one % symbol needed.)
width
Width given, height automagically selected to preserve aspect ratio.
xheight
Height given, width automagically selected to preserve aspect ratio.
widthxheight
Maximum values of height and width given, aspect ratio preserved.
widthxheight^
Minimum values of width and height given, aspect ratio preserved.
widthxheight!
Width and height emphatically given, original aspect ratio ignored.
widthxheight>
Shrinks an image with dimension(s) larger than the corresponding width and/or height argument(s).
widthxheight<
Enlarges an image with dimension(s) smaller than the corresponding width and/or height argument(s).
area@
Resize image to have specified area in pixels. Aspect ratio is preserved.
x:y
Here x and y denotes an aspect ratio (e.g. 3:2 = 1.5).
x:y^
remove rows or columns to achieve the given aspect ratio.
x:y#
add rows or columns to achieve the given aspect ratio.
{size}{offset}
Specifying the offset (default is +0+0). Below, {size} refers to any of the forms above.
{size}{+-}x{+-}y
Horizontal and vertical offsets x and y, specified in pixels. Signs are required for both. Offsets are affected by -gravity setting. Offsets are not affected by % or other size operators. Note that positive X and Y offsets are in the inward direction towards the center of the image for all -gravity options, except 'center'. For East, +X is left. For South, +Y is up. For SouthEast, +X is left and +Y is up. For center, the normal X and Y directional convention is used (+X is right and +Y is down).