This is a macOS-compatible Bash script that compresses .jpg and .jpeg images to ensure they are less than a user defined KiB threshold, using both native and external tools.
This script:
- Recursively finds all
.jpg/.jpegimages in a specified folder. - Tries compressing images using macOS’s built-in
sipstool. - If that fails, it uses ImageMagick to iteratively reduce image quality and resolution until the file size is strictly under 200 KiB.
- Saves all resized/compressed images in a separate
resized/folder, preserving your original files.
- macOS
- Bash
- ImageMagick (for fallback compression)
brew install imagemagickOpen the script and set the correct folder path in the FOLDER variable:
FOLDER="/your/path/to/images"chmod +x resize_images.sh
./resize_images.sh-
Processed images are saved in a subfolder called
resizedinside your original folder:/your/path/to/images/resized/ -
Each image is guaranteed to be < 200 KiB.
-
Original images are not modified.
The script uses a two-step strategy:
-
Primary Compression with
sips:- Tries
low, thennormal, thenhighquality settings.
- Tries
-
Fallback Compression with
ImageMagick:- Starts at 90% quality and 100% scale.
- Gradually reduces quality and then scale until the file is under 200 KiB.
This ensures the best possible image quality while keeping file sizes within the limit.
- Only supports
.jpgand.jpegfiles (case-insensitive). - Currently supports macOS only (due to
sipsandstat -f%zusage). - May reduce image quality or dimensions significantly if original images are large.
This script is free to use and modify. No warranties provided — use at your own risk.