Compress Images with One Line of Code
This guide shows you how to shrink image file sizes using a single command in your terminal or command prompt. You’ll learn the exact one-liner for both PNG and JPEG formats, what it does, and how to avoid common pitfalls. No prior coding experience is needed—just a computer with a command line interface.
What You’ll Need
- A computer running Windows, macOS, or Linux
- A terminal or command prompt (on Windows, you can use Command Prompt, PowerShell, or Windows Terminal)
- An image file you want to compress (PNG or JPEG recommended)
- Basic familiarity with navigating folders in the terminal (using
cdto change directories)
Step-by-Step
- Open your terminal. On macOS, press Cmd+Space, type “Terminal”, and hit Enter. On Windows, press Win+R, type
cmd, and press Enter. On Linux, use your distribution’s terminal emulator (e.g., Ctrl+Alt+T on Ubuntu). - Navigate to the folder containing your image. Use the
cdcommand. For example, if your image is in~/Pictures, typecd ~/Pictures(macOS/Linux) orcd C:\Users\YourName\Pictures(Windows). You can verify you’re in the right place withls(macOS/Linux) ordir(Windows). - Install the required tool (one-time). The one-liner uses a free, open-source tool called
ImageMagick. Install it if you don’t have it:- macOS:
brew install imagemagick(requires Homebrew) - Windows: Download and run the installer from the official ImageMagick website, then ensure “Add application directory to your system path” is checked during installation.
- Linux (Debian/Ubuntu):
sudo apt install imagemagick - Linux (Fedora):
sudo dnf install imagemagick
- macOS:
- Compress a single image with one line. Here’s the magic command:
magick input.jpg -quality 80 output.jpgReplace
input.jpgwith your original file name,80with your desired quality (0-100, lower = smaller file), andoutput.jpgwith the new file name. This works for PNG too—just change the extension to.png(though quality affects PNG differently; see Tips). - Compress multiple images at once. To compress all JPEGs in the current folder, use a loop. On macOS/Linux:
for f in *.jpg; do magick "$f" -quality 80 "compressed-$f"; doneOn Windows (PowerShell):
Get-ChildItem *.jpg | ForEach-Object { magick $_ -quality 80 "compressed-$_" } - Check the output. After running the command, you’ll see a new file (e.g.,
output.jpg) in the same folder. Compare its file size with the original usingls -lh(macOS/Linux) ordir(Windows). The new file should be smaller.
Common Mistakes
- Not installing ImageMagick. The
magickcommand won’t work until the tool is installed. If you get “command not found”, go back to step 3. - Overwriting the original. If you use the same name for input and output (e.g.,
magick image.jpg -quality 80 image.jpg), ImageMagick may refuse to overwrite the file or corrupt it. Always use a different output name. - Using the wrong extension. If your input is a PNG but you specify
.jpgas output, ImageMagick will convert the format, which can cause quality loss. Keep the same extension unless you intentionally want to convert. - Setting quality too low. A quality of 10 will make the file tiny but often unacceptably blurry. Start with 80 and adjust down gradually.
- Forgetting the loop syntax. The loop commands are case-sensitive and require proper quoting. Copy them exactly, and make sure you’re in the right directory.
Tips
- Use
-resizefor huge images. If the image is 4000px wide, reducing it to 2000px will often shrink the file more than any quality setting. Add-resize 50%to the command:magick input.jpg -resize 50% -quality 80 output.jpg. - For PNG, use
-stripand-define png:compression-level=9. PNG is lossless, so quality doesn’t work the same way. Instead, remove metadata and maximize compression:magick input.png -strip -define png:compression-level=9 output.png. - Batch with a simple loop. The loop examples above are your best friend for multiple files. Test on one image first to find the right quality level.
- Keep a backup. Always keep the original until you’ve verified the compressed version looks good.
- Use the
-verboseflag to see detailed information about the process:magick input.jpg -quality 80 -verbose output.jpg.
Troubleshooting
- “magick: command not found” — ImageMagick isn’t installed or not in your PATH. Reinstall it, ensuring you enable the PATH option on Windows. On macOS/Linux, verify with
which magick. - “magick: unable to open image” — The file name is wrong or the file isn’t in the current directory. Check spelling and use
ls/dirto list files. - Output file is larger than the original. This can happen with PNG files when you use
-quality(which does nothing for PNG) or with JPEGs that were already highly compressed. Try a lower quality value or resize the image. - Permission denied when writing output. You may not have write permissions in the folder. Try running the command in a different directory or use
sudoon Linux/macOS (but be cautious with sudo). - Loop doesn’t work on Windows Command Prompt. The
forloop syntax is different. Use PowerShell instead (typepowershellin cmd to switch).
Frequently Asked Questions
Does this work for all image formats?
ImageMagick supports dozens of formats (JPEG, PNG, GIF, WebP, TIFF, etc.). The command works the same, but quality settings differ. For GIF, use -coalesce first to avoid issues.
Will I lose image quality?
Yes, for JPEG, lowering quality reduces file size but also introduces compression artifacts. For PNG, the compression is lossless, so no quality is lost—only file size is reduced (though often less dramatically).
How much smaller will the file be?
It varies. A typical JPEG at quality 80 is about 30-50% smaller than the original. A PNG with compression level 9 can be 20-40% smaller. Test with your own images.
Can I compress images without installing anything?
Yes, but not with a single line of code. Online tools or built-in OS features (like Preview on macOS) can compress images, but they require manual steps. This guide’s one-liner requires ImageMagick.
Is ImageMagick free to use?
Yes, ImageMagick is free, open-source software distributed under a permissive license. You can use it for personal and commercial projects.
What does the -quality 80 actually do?
For JPEG, it sets the compression level on a scale of 0-100. 100 is best quality but largest file; 0 is worst quality but smallest. 80 is a good balance for most uses. For PNG, it’s ignored; use -define png:compression-level instead.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.