Using MacOS CLI to resize,convert image,Remove image background | Using sips
(Scriptable Image Processing System) on macOS
Definitive Guide to Using sips
(Scriptable Image Processing System) on macOS
The Scriptable Image Processing System (sips
) is a command-line tool that comes pre-installed on macOS. It's a versatile utility that allows you to manipulate images through scripts or direct command-line input. This guide will cover everything you need to know about sips
, from basic usage to advanced examples.
Table of Contents
Introduction to sips
sips
stands for Scriptable Image Processing System. It's a powerful command-line tool on macOS that allows you to perform various image manipulations such as resizing, converting formats, rotating, flipping, and more. It's particularly useful for automating repetitive tasks through scripting.
Basic Image Manipulation
Resizing Images
To resize an image to specific dimensions, you can use the -z
flag followed by the desired height and width.
Command:
sips -z <height> <width> /path/to/your/image.png
Example:
sips -z 80 80 /Users/hipster/Images/sample.png
This command resizes the image to 80x80 pixels.
Converting Image Formats
To convert an image from one format to another, use the --setProperty
flag to specify the new format.
Command:
sips --setProperty format <format> /path/to/your/image.ext --out /path/to/output_image.new_ext
Example:
sips --setProperty format jpeg /Users/hipster/Images/sample.png --out /Users/hipster/Images/sample.jpg
This converts a PNG image to JPEG format.
Rotating Images
To rotate an image, use the --rotate
flag followed by the angle in degrees.
Command:
sips --rotate <angle> /path/to/your/image.png
Example:
sips --rotate 90 /Users/hipster/Images/sample.png
This rotates the image 90 degrees clockwise.
Flipping Images
To flip an image vertically or horizontally, use the --flip
or --flop
flags.
Command:
sips --flip /path/to/your/image.png # Flips the image vertically
sips --flop /path/to/your/image.png # Flips the image horizontally
Example:
sips --flip /Users/hipster/Images/sample.png
Advanced Image Manipulation
Batch Processing
You can use sips
in scripts to process multiple images. For example, resizing all PNG images in a directory:
Script:
for image in /path/to/images/*.png; do
sips -z 100 100 "$image" --out /path/to/resized/$(basename "$image")
done
This script resizes all PNG images in the specified directory to 100x100 pixels and saves them in the resized
directory.
Adjusting Color
You can adjust the color of images using flags such as --matchTo
, --adjustColor
, and --setColor
.
Example:
sips --adjustColor sat 1.5 /Users/hipster/Images/sample.png
This increases the saturation of the image by 50%.
Adding Metadata
To add metadata to an image, use the --addMetadataTag
flag.
Example:
sips --addMetadataTag iptc.comment="This is a sample image" /Users/hipster/Images/sample.png
This adds a comment to the image metadata.
Practical Examples
Resizing for Xcode
If Xcode throws errors about image dimensions, you can quickly resize the images to meet the required dimensions.
Example:
sips -z 80 80 /Users/hipster/Images/layer-2-dpi.png --out /Users/hipster/Images/layer-2-dpi-resized.png
Creating Thumbnails
To create thumbnails for a batch of images, you can use a script:
Script:
for image in /path/to/images/*.jpg; do
sips -z 50 50 "$image" --out /path/to/thumbnails/$(basename "$image")
done
This creates 50x50 pixel thumbnails for all JPEG images in the specified directory.
Tips and Tricks
- Preview Changes: Use the
-p
flag to preview changes without modifying the original image. - Verbose Mode: Use the
-v
flag to get detailed output of the operations being performed. - Batch Renaming: Combine
sips
withrename
commands to rename images based on their properties.
Conclusion
The sips
tool is a powerful utility for image manipulation on macOS, offering a wide range of capabilities from basic resizing and format conversion to advanced color adjustments and metadata handling. Whether you're automating image processing tasks or just need a quick way to modify images from the command line, sips
is a versatile tool that can meet your needs. By following this guide, you should be well-equipped to leverage sips
for various image processing tasks. Happy scripting!