Good Image compressors/minifiers for the command line?

I have about 10k images I need to compress, browser tools have limits for size/number of images so I want something local I can setup and let it run for as long as it needs to, any good tools for that?
36 Replies
ErickO
ErickO3mo ago
Further info: - Image files: - PNG - JPG - Compression: - Lossless preferably, many of these images are old and quality isn't great to begin with - Bonus: - Video compressors?
dysbulic 🐙
dysbulic 🐙3mo ago
WebP (for lossless compression) or AVIF, right? For video compression I've been using H265 (High Efficiency Video Coding). It's the successor to x.264 & it defeinitely produces smaller files at times. In particular I recorded the output of ProjectM for about an hour. Lots of detailed visualizations for an hour while another call without a screen share went on. x.265 it's supported by web browsers yet, only x.264, so I'll sometimes transcode files when publishing to the web. That one hour of complex visual noise was a consistent ~10MiB / min in x.265. In x.264 the hour grew to over 20GiB before I killed it because it was only like halfway done.
Steam
projectM Music Visualizer
Visualize music with projectM! Experience psychedelic and mesmerizing visuals by transforming music into equations that render into a limitless array of user-contributed visualizations. projectM can detect audio input from any application (like Spotify or Apple Music) or you can use input from a microphone.Get StartedprojectM works out of the bo...
ErickO
ErickO3mo ago
I didn't even consider different formats das a good point, I can work with webp or avif but I'm using google photos to share these files and I don't think they support H265 you know of any tools that will let me batch convert all my jpegs/pngs to webp? or avif, whatever is available althoooough...if anyone wants to download the files they'll have the shitty webp experience 🤔 eh, still, if you have such a tool I'd like to know that'll be useful some day
Joao
Joao3mo ago
Imagemagick will work just fine for that.
if anyone wants to download the files they'll have the shitty webp experience
WebP and AVIF are widely supported beyond browsers. Even if it's not out of the box, there are libraries that can be installed to quickly provide support for these formats. At least on Linux, but I'm sure there are equivalents for MacOS and Windows.
ErickO
ErickO3mo ago
there are libraries that can be installed
yeeeh but average non-tech user situation here... the users that make memes about accidentally downloading a webP image and being enraged...basically those :PepeLaugh:
Joao
Joao3mo ago
I guess you could convert to multiple formats and share the most appropriate based on the audience? Otherwise, a good ol' JPEG should do the trick. The best way to "compress" images aka reduce size, is by reducing the image resolution anyway. Most people browser the web on their phones so you don't really need that much resolution
ErickO
ErickO3mo ago
yeeeah maybe, for now I'll do JPEG imma try lossless and see where that gets me, I don't mind smaller savings for now but if it's too little I might go lossy to a small degree like...95-97% quality, should be nearly the same as the original but like...30% smaller or so
Joao
Joao3mo ago
With imagemagick the command would look something like this:
convert input_image.png -q 75 output_image.jpg
convert input_image.png -q 75 output_image.jpg
-q for quality of course, and for format conversion just specify the extension on the output.
ErickO
ErickO3mo ago
batch option? or I need to script it?
Joao
Joao3mo ago
Yeah, best use a script because it's a bit verbose. Just a sec I have one ready for you
ErickO
ErickO3mo ago
given there's a q I imagine it not only converts but also compresses?
Joao
Joao3mo ago
I suppose a disclaimer is in order, offered "as is" without any guarantees, etc, 😄 but here's what I use: https://codeberg.org/D10f/dotfiles/src/branch/master/bin/img2format
ErickO
ErickO3mo ago
as long as it's not lua I can edit it if needed
Joao
Joao3mo ago
Yes, -q for quality (compression) and you can do things like -resize 800x600 to reduce the size (specify only one of these widthxheight to preserve aspect-ratio) and the conversion of the file type is based on the name of the output file. If the output is output_img.webp it will convert to webp automatically.
ErickO
ErickO3mo ago
much more complete than what I was about to write :KEKW: (scuffed script 😎)
Joao
Joao3mo ago
It's written in Bash and it's just a wrapper for imagemagick so it's fairly straight forward. I attempted to add a rounded corners function which I use for screenshots but ... don't use it 😄 Yeah, I welcome any PR's if you like 😄 give it a test run first on some images just to confirm it works fine. I use it mostly for screenshots so not very sensitive information for me, that's why I haven't really tested it thoroughly. But towards the end of the script you will find the juicy part that actually does the job, the rest is jsut the wrapper.
ErickO
ErickO3mo ago
I dun know much about bash but if I get the time I'll try to add stuff 👍 thanks mate
Joao
Joao3mo ago
Well let me know if you run into issues anyway, I'd like to improve this if I can
ErickO
ErickO3mo ago
will do, thanks!
Joao
Joao3mo ago
You might want to modify this at the end (line 119):
convert "$file_path" ${convert_args[@]} "${file_name}.${format}"
convert "$file_path" ${convert_args[@]} "${file_name}.${format}"
To give it some sort of prefix to the output file name, just to avoid any accidental overwriting. For example:
convert "$file_path" ${convert_args[@]} "${file_name}-converted.${format}"
convert "$file_path" ${convert_args[@]} "${file_name}-converted.${format}"
ErickO
ErickO3mo ago
that makes sense
Joao
Joao3mo ago
I just realized that it would be useful to add an option to specify a location to store the images, I'll try to add it later. Also, the long form options don't work yet (--quality instead of -q, etc.), even though the appear in the help text, but I intend to add support for those as well.
ErickO
ErickO3mo ago
oh no, can't use long form options (nothing was lost :Kappa: )
Joao
Joao3mo ago
Just in case you tried to used them and nothing happened 😅
ErickO
ErickO3mo ago
fair :PepeLaugh: I tried h.264 and h.265 btw - Original MP4 h.264: - 1.64GB - Compressed MP4 .264 (medium speed): - 503.1MB - Compressed MP4 .264 (slow speed): - 469.3MB - Compressed MP4 .265 (medium speed): - 281.2MG - Compressed MP4 .265 (slow speed): - 325MB (what?) the difference between medium and slow compress speed is EXPONENTIAL, seriously the slow takes ages and the medium is not even half that time for very little improvement...I'm good with medium speed thanks :PepeLaugh: .265 clearly a biiig improvement, actually insane that a file can go from 1.64GB to 281MB 🤯 quick Q, Joao uh...how am I supposed to use your command for batch compression? I'd try reading the bash but it's gibberish to me ngl I used globbing patterns to get it done so like folder/*.jpg is that right? or was there another method
Joao
Joao3mo ago
Yeah, using a glob pattern should work, with the only requirement that it must come as the last argument. For instance:
$ img2format -f png -o /home/joao/Desktop /home/joao/Downloads/*.png
$ img2format -f png -o /home/joao/Desktop /home/joao/Downloads/*.png
I added the -o option to specify the output folder where you want to save the converted images (originals are preserved) since we last talked btw. Also note that I intended this to convert images between formats so you need to specify the -f flag always even if it's the same. You can do this if you want to resize them instead or reduce the quality instead, for example:
$ img2format -f png -s 800x600 -o /home/joao/Desktop /home/joao/Downloads/*.png
$ img2format -f png -q 75 -o /home/joao/Desktop /home/joao/Downloads/*.png
$ img2format -f png -s 800x600 -o /home/joao/Desktop /home/joao/Downloads/*.png
$ img2format -f png -q 75 -o /home/joao/Desktop /home/joao/Downloads/*.png
For compression specifically use -q and a value between 1 and 100, where 100 means leaving the original untouched since it refers to quality.
ErickO
ErickO3mo ago
yup, that's what I did I tested with 100 first and somehow the original was 12MB and the converted was 14MB huuuuh :PepeLaugh: anyway, I went through all my images at -q 95 and it worked great 👍 all in all between lossless and lossy compression of my images and a few videos here and there I saved about 7GB of space
Joao
Joao3mo ago
Oh, that's surprising... I guess it depends on the type of image and it contains too many hard to compress artifacts. 🤔
Joao
Joao3mo ago
The documentation talks about how the quality is calculated and how to best apply it: https://imagemagick.org/script/command-line-options.php#quality
ImageMagick
ImageMagick is a free and open-source software suite used for editing and manipulating digital images. It provides a versatile set of tools for various image-related tasks. You can use ImageMagick to resize, convert, and apply various transformations to images. It supports a wide range of file formats, including JPEG, PNG, GIF, TIFF, and Ultra HDR.
ErickO
ErickO3mo ago
mayhaps, who knows I went with -q 95 anyway
Joao
Joao3mo ago
Oh, well, glad it all worked out in the end anyways Can I ask what formats did you convert from/to? If any, or did you only apply compression?
ErickO
ErickO3mo ago
I went jpg to jpg :PepeLaugh: technically I went .JPG and .jpg to .jpg
Joao
Joao3mo ago
The casing in this case won't make a difference but being jpeg format original might explain it, as it already is compressed to some degree. But I would say just play with the settings a bit should be fine.
dysbulic 🐙
dysbulic 🐙3mo ago
Why not .webp?
ErickO
ErickO3mo ago
sharing files with my family, webp kinda shit natively if u wanna download them they're non-techy so rather not add friction
tharun
tharun3mo ago
Don't know if this is what you're looking for, but there's a site called image compress. It's free and unlimited. https://compressimage.io
Compress Images Online
Compress Images Online | Compress JPG & PNG Images
Compress image online within seconds. The Fastest image compressor that lets you compress jpg and compress png without any limits.