Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Batch Compress Images from Command Line

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to compress multiple images at once using free command-line tools. You'll learn two practical methods—one using ImageMagick (cross-platform) and one using the built-in tools on macOS and Linux. By the end, you can shrink a folder of photos or screenshots without opening a single image editor.

What You'll Need

  • A terminal (Command Prompt on Windows, Terminal on macOS/Linux).
  • ImageMagick installed (for the primary method). On macOS: brew install imagemagick. On Ubuntu/Debian: sudo apt install imagemagick. On Windows: download from imagemagick.org and install.
  • Basic comfort with navigating folders in the terminal (cd and ls).

Step-by-Step

  1. Open your terminal and navigate to the folder containing your images. For example: cd ~/Pictures/vacation. Verify you're in the right place with ls (or dir on Windows).
  2. Create a new folder for the compressed output to keep originals untouched. Run: mkdir compressed.
  3. Compress all JPEG images in the current folder with ImageMagick's mogrify command. The simplest form is: mogrify -path compressed -quality 85 *.jpg. This creates compressed versions of all .jpg files in the compressed folder, with quality set to 85% (a good balance between size and quality).
  4. For PNG files, use a different approach because PNG is lossless. You can reduce file size by reducing colors or using pngquant (install separately). With ImageMagick alone, you can do: mogrify -path compressed -define png:compression-level=9 *.png. This maximizes compression but may not shrink files dramatically.
  5. Resize images as you compress to save even more space. For example, to limit the width to 1920 pixels: mogrify -path compressed -resize 1920x -quality 85 *.jpg. The -resize 1920x keeps the aspect ratio and only sets the width.
  6. Use a loop for more control (especially on macOS/Linux). If you need to keep original filenames and process each file individually, use a for loop: for img in *.jpg; do convert "$img" -quality 85 "compressed/$img"; done. This is slower but lets you add extra options per file.
  7. Compress all images in subfolders as well by using find with mogrify. On macOS/Linux: find . -name "*.jpg" -exec mogrify -path compressed -quality 85 {} \;. This processes every JPEG in the current directory and all subdirectories, placing results in the compressed folder (which must exist).
  8. Check the results by comparing file sizes. Use ls -lh compressed to see the new sizes. Open a few images to verify quality is acceptable.

Common Mistakes

  • Forgetting the -path option: Without it, mogrify overwrites your original files. Always use -path to specify an output folder.
  • Using convert for batch jobs: convert processes one file at a time. For multiple files, use mogrify or a loop. mogrify is designed for batch operations.
  • Not quoting filenames with spaces: If your files have spaces (e.g., "my photo.jpg"), you must quote them in loops: "$img". Unquoted, the shell splits the name into multiple arguments.
  • Setting quality too low: Quality 50 or below can cause visible artifacts, especially on text or gradients. Start at 80-85 and adjust.
  • Ignoring the output folder existence: mogrify -path will fail if the target folder doesn't exist. Create it first with mkdir.

Tips

  • Test on a few images first before compressing an entire library. Run the command on 3-4 files and inspect the quality.
  • Use a quality of 80-85 for photos—it's often visually indistinguishable from the original but significantly smaller.
  • For web use, consider resizing to the maximum display size. A 4000px wide photo is overkill for a blog post. Resize to 1920px or 2560px to save megabytes.
  • Strip metadata to save extra bytes: add -strip to remove EXIF data (camera info, GPS, etc.). Example: mogrify -path compressed -strip -quality 85 *.jpg.
  • Use jpegoptim on Linux for even better JPEG compression. It's a dedicated tool that often produces smaller files than ImageMagick at the same quality.
  • Convert PNG to JPEG when appropriate: If a PNG is a photo (not a screenshot or graphic with transparency), converting to JPEG at quality 85 can drastically reduce size. Use mogrify -path compressed -format jpg -quality 85 *.png.

Troubleshooting

  • "command not found": ImageMagick is not installed. Install it via your package manager (brew, apt, or the Windows installer). Alternatively, use sips on macOS (see FAQ).
  • "mogrify: unable to open image": The file might be corrupt or not an image. Check the file type with file filename.jpg.
  • Output folder not created: mogrify -path does not create the folder. Run mkdir compressed first.
  • Files are not compressed: If the output size is similar or larger, the image may already be highly compressed, or you're using a high quality setting. Try lowering quality or resizing.
  • Permission denied: You might not have write access to the output folder. Use a folder in your home directory or run with sudo (be careful).
  • Loop with spaces fails: Ensure you use "$img" in the loop, not $img. Also, use for img in *.jpg which handles spaces correctly in the glob.

Frequently Asked Questions

Can I compress images without installing anything?

Yes. On macOS, use the built-in sips tool: sips -s formatOptions 85 -s format jpeg *.jpg --out compressed/. On Linux, you might have convert from ImageMagick pre-installed, but not always.

What's the difference between convert and mogrify?

convert processes a single image and outputs a new file. mogrify is designed for batch processing and can overwrite originals (unless you use -path). For batch tasks, use mogrify.

How do I compress images to a specific file size?

ImageMagick doesn't directly target a file size. You can estimate by adjusting quality and resizing. For precise control, use a tool like jpegoptim with the --size option, or use a GUI app.

Will compression reduce image quality?

Yes, JPEG compression is lossy—higher compression (lower quality) reduces file size but introduces artifacts. PNG compression is lossless, so quality remains identical, but file size reduction is limited.

Can I compress images in subfolders and keep the folder structure?

Yes, but it's more complex. You can use a shell script with find to recreate the directory tree. A simpler approach is to use zip with compression, but that doesn't compress the images themselves.

Is there a way to compress all images regardless of extension?

Yes, you can use wildcards like * or *.png *.jpg *.jpeg. Be careful: mogrify will try to process any file, so specify the extensions you want.