13

I am looking for a command line lossless jpeg compression tool which will have the following functionality:

  • be a command line tool with no gui interface
  • I can provide list of folders (where my potentially unoptimized images are), it will recursively goes and optimize all images inside of these folders and subfolders
  • the images will be saved with the same name as they were before (substituting old unoptimized image for a new one)

Also it is not really important, it would be nice if the tool could

  • Give a report (how much space per file / in total was saved)
  • Could resize images as well
  • Should properly handle big amount of files (> 1000)
  • Remove metadata

Answer to Nick Wilde:

I do not know whom should I trust: you or people from google who created pageSpeed. When I run this jpeg through pageSpeed it says

Losslessly compressing http://upload.wikimedia.org/.../Lake_mapourika_NZ.jpeg could save 16.8KiB (4% reduction).

P.S. the reason to use a tool is not to save some space on my machine, the images are on the server, so optimizing images will help to save bandwidth for users viewing images. For this reason images should be viewable and archiving them is not an option.

12

After hours of playing with bash, I found a way how Google is doing it. It is using one of these 2 programs:

Both of them support lossless and lossy conversion. I tried the second one, in Ubuntu as jpegoptim Install jpegoptim http://hostmar.co/software-small.

Than to do what I want I have to do the following:

find MyDirectory/ -type d -exec sh -c '
    ls "$0"/*.jpg 2>/dev/null && jpegoptim --strip-all -t "$0"/*.jpg
' {} \;

This is doing really close to what I want. The only problem is that I see a lot of output about each file compression and I can not get rid of it with -q because it also get rid of summary. But this is the closest I was able to get with my just learned bash "skills". I asked a follow-up question on Ask Ubuntu.

It is really fast. It took it < 5 sec to process few thousands of files.

  • 1
    Have you tried re-directing output? Simply add >/dev/null to the end of your jpegoptim command line to re-direct STDOUT to "data nirvana". Potential errors (and everything else sent to STDERR) will still be shown. – Izzy Mar 21 '14 at 9:47
  • I assume that >/dev/null will get rid of all output. This is not what I want, because I can achieve the same thing with -q – Salvador Dali Mar 21 '14 at 9:49
  • 1
    You don't detail on the "superflous output". But if the stuff you want to get rid of is easily separated from the stuff you want to keep via keywords, an alternative would be | grep -v <not-this-keyword> | grep -v <this-neither> [...], which then would suppress all lines containing the mentioned keywords. – Izzy Mar 21 '14 at 9:52
  • I haven't looked at what jpegoptim outputs. What kind of summary to you want to keep? Why are you calling it separately for each directory? You may want to ask about that part on Unix & Linux. – Gilles 'SO- stop being evil' Mar 21 '14 at 10:59
  • @Gilles I already asked this on ubuntu stackexchange. I am calling separately on each directory, because if I will call it on the parent directory, it will tell me something like 'sorry no images found' – Salvador Dali Mar 21 '14 at 11:08
2

ImageMagik convert allows command line usage, batch processing and will convert your jpegs to jpeg2000 which may save you some space and has several other options. Unless you have made a backup first I would recommend not overwriting existing files until you have checked the conversion worked as things can go wrong with any tool.

Note that zip or targz individual jpegs normally results in an increase in size if you need to save space then tar directories and tar does have the option --remove-files to remove files as they are added to the archive. While it will not make the files smaller you will only waste space after one file rather than after each file.

For Info:

$ convert -list compress

B44 B44A BZip DXT1 DXT3 DXT5 Fax Group4 JBIG1 JBIG2 JPEG JPEG2000 Lossless LosslessJPEG LZMA LZW None Piz Pxr24 RLE Zip RunlengthEncoded ZipS

  • Thanks. If you are interested, I found how google is doing this. – Salvador Dali Mar 21 '14 at 7:08
  • I think it's more reliable to check convert -list format instead. I have JPEG2000 in the compress options but not in the formats, and the result is convert -compress jpeg2000 00001.tif 00001.pdf convert: delegate library support not built-in 00001.pdf' (JP2) @ error/pdf.c/WritePDFImage/1283.` – Nemo Oct 6 '15 at 13:04
  • (As suggested by imagemagick.org/discourse-server/viewtopic.php?p=120932#p120932 ) – Nemo Oct 6 '15 at 13:50
  • Note, you can define a target size with jpeg:extent, e.g. convert -define jpeg:extent=400KB. imagemagick.org/discourse-server/viewtopic.php?t=15386 – Nemo Jan 25 '16 at 11:08
  • convert is never lossless if you output to a lossy format. therefore this part is off-topic. – Sarge Borsch Dec 18 '18 at 15:50
-1

JPEG IS a lossy format - they cannot be compressed via JPEG compression losslessly. So there is no tool that can do that.

That being said there are likely tools that can optimize JPEG images - ie remove junk data though that would be minimal size savings.

Or another method would be to individually zip/7z/gzip (etc) compress them and delete the originals.

If either of those options are desired edit or create a new question.

  • Thanks for your answer. Please check the edit to my question. – Salvador Dali Mar 21 '14 at 0:55
  • Unfortunately zipping jpegs individually increases the size of them as there is an overhead and they are already compressed. Zipping/targz can save space if you zip a folder full as the wasted space on the disk after each image is saved. – Steve Barnes Mar 21 '14 at 5:29
  • 3
    Guys, this is a totally wrong answer. Not only it suggests a wrong tool to do the job, it also conveys wrong information about how JPEG works. Do not understand why someone gave it +1 – Salvador Dali Mar 21 '14 at 10:09
  • A JPEG at 100% quality is loss-less. – Elliott Frisch Mar 21 '14 at 20:00
  • 2
    @ElliottFrisch No, JPEG with 100% quality is still lossy - SO: Is Jpeg lossless when quality is set to 100?. JPEG2000 has lossy and lossless modes, but that's something different. – Volker Siegel Jul 13 '14 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.