简体   繁体   中英

How to resize images using terminal on Mac OSX?

I need a simple and free way to resize images and do batch jobs, if necessary. Free image manipulation software has been trickier to use than it should be.

As pointed out by LifeHacker, the following command will do this very easily:

sips -Z 640 *.jpg

To quote their explanation:

"So what's happening? Well, "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. Just be sure to make a copy first if you want to preserve their larger size as well."

Source: http://lifehacker.com/5962420/batch-resize-images-quickly-in-the-os-x-terminal

imagemagick helps:

$ convert foo.jpg -resize 50% bar.jpg

There are a lot more things it can do, including the conversion between formats, applying effects, crop, colorize and much, much more.

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.

mogrify -resize 60% *

magic trick for itunesconnect :)

    mkdir ./iPhone5-5-Portrait
    sips -z 2208 1242 *.jpg -s formatOptions 70 --out ./iPhone5-5-Portrait
    sips -z 2208 1242 *.png --out ./iPhone5-5-Portrait

Additionally of @grepit reply

The correct syntax is:

magick mogrify -resize 60% *

And you need to install ImageMagick , the easiest way is using homebrew:

brew install imagemagick

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.

Install imagemagick,

brew install imagemagick

All examples are given in the official site . For resizing images, check this section .

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,

magick mogrify -path out -resize 1280x1080\> *

Other useful options for -resize (copied from the official site ),

size 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 (eg 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM