“Compress without losing quality” gets thrown around loosely, so it’s worth being honest up front: PNG really can be made smaller with zero pixel change, while JPG is already compressed, so the free wins there come from stripping junk and re-optimizing what’s there. Push a JPG further and you’re trading a little quality, whether a tool admits it or not.
The good news is you control exactly where that line sits, and you do it for free without uploading a single file. WSL gives you the three tools that handle this best — optipng for lossless PNG, jpegoptim for JPG, and ImageMagick when you want one tool for everything.
Don’t have WSL yet? Our WSL install guide gets you there in one command.
Install the optimizers
sudo apt update && sudo apt install -y jpegoptim optipng imagemagick
That’s three tools: jpegoptim (JPG), optipng (PNG), and imagemagick (magick/convert, format-agnostic).
Lossless PNG compression with optipng
PNG is where “no quality loss” is literally true. optipng repacks the image data more efficiently; the pixels are untouched.
optipng image.png
Push it harder with a higher optimization level (slower, sometimes smaller):
optipng -o5 image.png
Whole folder:
for f in *.png; do optipng -o5 "$f"; done
The savings depend entirely on how the PNG was originally saved — files exported by some apps shrink a lot, already-optimized ones barely move.
JPG: strip metadata first (genuinely lossless)
The cleanest JPG win is removing metadata without re-encoding the image. That’s lossless — the pixels don’t change:
jpegoptim --strip-all image.jpg
--strip-all drops EXIF, comments, and thumbnails. The image looks identical; the file is smaller. As a bonus, it also clears embedded data like camera info and GPS location. (For thorough metadata removal across formats, see remove image metadata.)
JPG: go a bit smaller (visually lossless)
When stripping isn’t enough, cap the quality. jpegoptim -m85 only re-compresses files above quality 85 down to it — usually invisible, noticeably smaller:
jpegoptim -m85 --strip-all image.jpg
Whole folder:
for f in *.jpg; do jpegoptim -m85 --strip-all "$f"; done
Lower the number for smaller files, but check the results — somewhere around 80–85 is the usual sweet spot for photos.
One tool for everything: ImageMagick
If you’d rather not install several utilities, ImageMagick handles both formats and writes to a new file (so it doesn’t overwrite the source):
magick input.jpg -strip -quality 85 output.jpg
-stripremoves metadata.-quality 85sets JPG quality.
For PNG, ImageMagick can also reduce size, though optipng usually beats it for pure lossless packing:
magick input.png -strip output.png
Which tool, which job
| optipng -o5 f.png | Lossless PNG repack — identical pixels |
|---|---|
| jpegoptim --strip-all f.jpg | Lossless JPG — strips metadata only |
| jpegoptim -m85 --strip-all f.jpg | Visually lossless JPG — small quality cap |
| magick in -strip -quality 85 out | One tool, writes a new file |
| for f in *.jpg; do ... done | Batch a whole folder |
A sensible workflow
For a folder of mixed images you want to slim down for the web without visible loss:
cp -r originals/ work/ && cd work/
for f in *.png; do optipng -o5 "$f"; done
for f in *.jpg *.jpeg; do [ -e "$f" ] && jpegoptim -m85 --strip-all "$f"; done
Copy first, optimize PNGs losslessly, optimize JPGs to a visually-lossless cap. Compare a few against the originals before you trust the batch.
Wrapping up
“Without losing quality” is real for PNG and mostly about stripping and smart re-optimization for JPG. optipng repacks PNGs losslessly, jpegoptim --strip-all trims JPG metadata with zero pixel change, and -m85 gives a visually-lossless extra squeeze when you need it. ImageMagick covers both if you want a single tool.
All of it is free, batchable, and runs in WSL — so your images never get uploaded to a compressor site. Work on copies, compare the output, and you’ll trim folders of images safely.