You're asking for the "simplest way to compress a PNG file further" while staying on Ubuntu and using the command line.
While ffmpeg can handle PNGs (e.g., converting video frames to PNG, or converting a PNG to another format), it's generally not the primary or most effective tool for optimizing an existing PNG file for size reduction while keeping it in the PNG format. For that, specialized PNG optimization tools are far superior.
Here are the simplest and most effective ways, categorized by whether they are lossless or lossy:
1. Lossless PNG Compression (Recommended First)
These tools re-encode the PNG data more efficiently without any loss of image quality. This is often the best starting point.
Install:
sudo apt update
sudo apt install optipng pngcrush zopflipng
a. optipng (Most Common & Effective Lossless)
This is typically the first tool to try for lossless optimization.
optipng -o7 input.png -out output.png
-o7: This specifies the optimization level (0-7). 7 is the highest and performs the most extensive (and slowest) optimization. You can try o5 or o6 for a good balance.
-out output.png: Specifies the output file name. If omitted, optipng will overwrite the original file (so be careful and back up first!).
Example to overwrite original (use with caution!):
optipng -o7 my_image.png
b. pngcrush (Another Strong Lossless Option)
pngcrush is very powerful and can sometimes achieve slightly better compression than optipng, though it might be slower.
pngcrush -rem allb -reduce -brute input.png output.png
-rem allb: Removes all ancillary chunks (metadata, gamma info, etc.) which are usually not needed and can add to file size.
-reduce: Reduces the number of colours if possible without loss.
-brute: Tries many different compression methods to find the smallest file (this can be very slow). For a quicker pass, you can omit -brute.
c. zopflipng (Maximum Lossless Compression - Very Slow)
If you want the absolute smallest possible PNG file without any quality loss, zopflipng (from Google) is excellent. It uses the Zopfli algorithm, which is highly efficient but also much slower than optipng or pngcrush.
zopflipng -m input.png output.png
-m: Specifies "more iterations," leading to better compression but slower processing.
- Note:
zopflipng usually outperforms optipng and pngcrush in terms of final file size, but it takes significantly longer, especially for larger images.
2. Lossy PNG Compression (Reducing Colors - Still PNG)
This method reduces the number of colors in the PNG (color quantization). This can drastically reduce file size, but it is a lossy process, meaning some visual fidelity is lost, especially in images with many subtle color gradients (like photos). However, for many web graphics or screenshots, the quality loss is negligible, and the size savings are huge.
Install:
sudo apt install pngquant
a. pngquant (The Go-To for Lossy PNG)
This is the most common and effective tool for lossy PNG compression.
pngquant --quality=65-80 input.png --output output.png
--quality=65-80: This is the most important part. It sets a minimum (65) and maximum (80) quality threshold. pngquant will try to compress the image to a quality within this range.
- Higher numbers (e.g.,
80-90) mean better quality, larger files.
- Lower numbers (e.g.,
40-60) mean lower quality, smaller files.
- Experiment with this value! A good starting point is often
60-80.
--output output.png: Specifies the output file name. pngquant usually appends -fs8.png to the original name if you don't specify an output.
Example:
pngquant --quality=70-85 original_image.png --output compressed_image.png
3. Converting to a Different Lossy Format (No Longer PNG)
If the goal is maximum compression and you don't need the PNG format specifically (e.g., you don't need alpha transparency for a photo, or you can use WebP), converting to another format can yield even smaller files. This is where ffmpeg (and imagemagick's convert) shines.
Install (if you don't have them):
sudo apt install ffmpeg imagemagick webp
a. Converting to WebP (Modern, Good for Photos & Graphics with Transparency)
WebP generally offers better compression than JPEG for similar quality, and it supports transparency like PNG.
Using ffmpeg:
ffmpeg -i input.png -q:v 80 output.webp
-q:v 80: Sets the video quality (for image encoding, it acts as the image quality). 0 is worst, 100 is best. 80-90 is a good range.
Using cwebp (often better for WebP specifically):
cwebp -q 80 input.png -o output.webp
-q 80: Sets the quality factor (0-100).
b. Converting to JPEG (Great for Photos - No Transparency)
JPEG is excellent for photographic images but does not support transparency.
Using ffmpeg:
ffmpeg -i input.png -q:v 85 output.jpg
-q:v 85: Quality factor (0-100). Higher is better quality, larger file.
Using convert (from ImageMagick - Very versatile):
convert input.png -quality 85 output.jpg
-quality 85: Quality factor (0-100).
Simplest Recommendation Workflow:
-
Try optipng first (lossless):
optipng -o7 your_image.png
(This will overwrite the original, so make a backup first or use -out new_image.png)
-
If still too large, try pngquant (lossy PNG):
pngquant --quality=70-85 your_image.png --output your_image_quantized.png
Compare your_image_quantized.png with your_image.png visually and for size. Adjust quality as needed.
-
If you need even smaller and don't strictly need PNG, convert to WebP:
cwebp -q 80 your_image.png -o your_image.webp
Choose the method that balances file size reduction with your acceptable level of quality loss and format requirements.