guetzli icon indicating copy to clipboard operation
guetzli copied to clipboard

Request for Documentation / Clarifying How to Best Use the Tool

Open DeeDeeG opened this issue 7 years ago • 9 comments

Hi all,

This is a very cool project, and I think many of us looking at Guetzli are interested and excited by its promise of better compression and higher-quality images.

I have some notes on what I found confusing as a new user. Answering these questions would help users make better use of the tool, with better outcomes. (Obviously just answering these questions here would help me personally, but it would be great if they would make it into some official documentation. Nonetheless, informally discussing it here is probably an okay first step toward identifying what should be documented.)

My main questions are:

  • What does Guetzli do on the default setting? i.e. no flags on.

    • Does it read the Quality number used on the original image, like GIMP does when exporting as JPG? (answered: it defaults to Quality 95: https://github.com/google/guetzli/issues/76#issuecomment-287605815)
    • The file is compressed by some amount for a given --quality setting. Is Guetzli attempting to output a file that is of equivalent quality (extremely low difference as per Butteraugli) yet as much smaller as possible given that constraint? (Answered in code comments: Quality for a given Q (84-110) in Guetzli is determined by attempting hit a target Butteraugli score, which is set to match the Butteraugli score that libjpeg-turbo achieved with the same given Q number (0-100), averaged over a set of test PNGs: guetzli/quality.cc#L31)
  • Just for curiosity's sake, how much difference of Butteraugli score is it willing to tolerate compared to its target for the specified Q

    • Is it loose, accepting a wide margin above/below the target Butteraugli score, in order to provide output at a very low filesize? Or is it conservative, and only willing to compress to a smaller size if the visual difference (actual Butteraugli score compared to the target score) will be negligible?
    • How does Guetzli decide what info to optimize/throw away in the name of file-size (what are the algorithms), how is this smarter than other tools? How can we play into this compression strategy and help it succeed?
      • Do's and Don'ts guide to compression with Guetzli?
      • How should my photos be handled or converted before using Guetzli, for best results?
  • Is it bad to run the program with no flags and expect miracles?

    • (A rhetorical question... But this would be a good thing for myself and probably most lay-person users to know. How should we approach getting good compression and respectable image quality out of Guetzli? What are the limits of running without flags? How to dip our toes into the Q settings? Are other tools better for novices such as myself?)
    • Is there a good guideline for what the --quality Q value should be?
    • With respect to the often-touted 20-30% file-size reduction figure, how should we achieve that? At what quality value should we be outputting to see that reduction?

Thank you for your consideration.

DeeDeeG avatar Apr 23 '17 18:04 DeeDeeG

Here are my 2 cents ...

How does Guetzli decide what info to optimize/throw away in the name of file-size (what are the algorithms), how is this smarter than other tools? How can we play into this compression strategy and help it succeed?

Guetzli uses feedback from butteraugli to decide what data to throw away.Read Guetzli paper for more info

How should my photos be handled or converted before using Guetzli, for best results?

Note that Guetzli is designed to work on high quality images. You should always prefer providing uncompressed input images (e.g. that haven't been already compressed with any JPEG encoders, including Guetzli). While it will work on other images too, results will be poorer.

For best result the input images should be in a lossless format such as PNG

Is it bad to run the program with no flags and expect miracles?

Use it on the PNG with no flag and it will work fine if you have the resources(i.e enough RAM and CPU on your server)

Note: Guetzli uses a large amount of memory. You should provide 300MB of memory per 1MPix of the input image.

Note: Guetzli uses a significant amount of CPU time. You should count on using about 1 minute of CPU per 1 MPix of input image.

Is there a good guideline for what the --quality Q value should be? With respect to the often-touted 20-30% file-size reduction figure, how should we achieve that? At what quality value should we be outputting to see that reduction?

The default quality (95) would give you perfect images and will give you the file-size reduction you want.The quality flag can be used to get smaller filesize at the cost of quality

khavishbhundoo avatar Apr 25 '17 12:04 khavishbhundoo

That's great, thank you.

On the point of:

Are there better tools for novices such as myself

In my view, Mozjpeg (or any other variant of libjpeg) is probably the best go-to encoder (for web assets) for absolute beginners. It is fast, and produces decent images. Filesizes can even be smaller than Guetzli, because Q below 84 is allowed. You can do lots of trial and error and learn the ropes. I basically spent a day learning the jpeg compression workflow and I found Mozjpeg made it convenient to learn these things. I produced ~100 encodes in my image set at different Q, and I'm not sure I could have even begun to approach that number of encodes, given the runtime Guetzli has right now.

Until Guetzli is optimized, Guetzli is for a fairly specialized use-case. (high-resource usage, only allows very-high-quality settings). I think it kind of implicitly assumes you know what you're doing already, and that opting for Guetzli is an informed choice.

Now that I'm familiar with the workflow, I am in a better position to understand if Guetzli is better than Mozjpeg, so honestly it's a win-win in a way.

(Not saying Guetzli has to evangelize for Mozilla in their README.md! Just making a note of my experiences here. Devs can make/not make whatever documentation they want to.)

DeeDeeG avatar Apr 25 '17 18:04 DeeDeeG

@DeeDeeG

I would recommend using ImageMagick or GraphicsMagick. Under the hood they both use libjpeg but you can compile it to use libjpeg-turbo.You should be able to generate JPEG also at any quality you want + do all kinds of image manipulations.Allowing quality less than 84 would defeat the purpose of this encoder as it aim is to provide JPEG of good quality at best file size.

khavishbhundoo avatar Apr 25 '17 19:04 khavishbhundoo

I found out some things that could help guide users.

Regarding PNG vs JPEG:

If your input is a PNG, in some cases it is best to keep it a PNG. This is especially true for images with large areas of the exact same color.

If a typical fast JPEG encoder (such as the one built into GIMP or built into ImageMagick or Photoshop...) exports a JPEG larger than your source PNG, no matter what settings you try, then Guetzli can't help.

Regarding ways to handle your input images before using Guetzli:

Start with a high quality, unprocessed JPEG or PNG (ideally straight from the camera or straight from the software that initially produced it, and not compressed or re-compressed), when possible.

But feeding this directly into Guetzli will often produce large files! There are ways to reduce the output filesize without throwing away noticable quality.

Often the easiest way is to pick a lower Quality setting in Guetzli. Anywhere in the allowed range may produce a good result (85 - 95 usually are best, higher and lower quality settings are RARELY needed).

But that's not the only way!

Another way to reduce output filesize is to scale down or crop down your input image. You will be able to tell by looking at the image whether or not the resulting image is too small or too cropped down. Reducing the number of pixels in your output image reduces the size. If you're happy with your image the way it is, and don't want to change how it's framed, try scaling down somewhat.

Another option, especially for very high resolution images, is gaussian blur, or selective gaussian blur. Gaussian blur with a radius of 1px will reduce image complexity, and give a softer look. Do this, then scale down for lots of filesize savings, and not much noticeable difference in quality (if your source is detailed enough and large enough to begin with).

Selective Gaussian blur preserves contrasty parts of the image, while blurring and reducing complexity of less-contrasty areas. If your image is a high-resolution image from a digital camera, and it has noise that doesn't add clarity to the scene, selective gaussian blur will get rid of some of the noise while preserving edges and major details. This reduced complexity is also easier to compress. The biggest downside of this is that too wide a blur radius can over-smooth and blur out your picture. I keep it at around 2px radius and 11 or so max diference (GIMP).

Selective gaussian blur artificially bumps up contrast in your image. If your image was washed out or regular/accurate contrast to begin with, this artificially boosted contrast may look BETTER to they eye. People often like to look at contrasty images. If it's already over-contrasted, it will probably make the image look worse. Too much contrast looks punched up, blown out, and artificial. In any case, be aware that contrast goes up when using selective gaussian blur.

For super high resolution camera photos, I highly recommend some form of Gaussian blur then scaling down in order to subtly reduce complexity, and get rid of some pixels, all the while keeping your image looking nice. You can then pick the correct quality setting for Guetzli (trial and error? Do 85, 90, and 95 to start, or just 85 and 95, as upper and lower bounds, then see if the filesizes and visible quality are acceptable and work from there.)

(Tip: other encoders allow chroma subsampling. This encodes lower resolution data for color than for brightness. It saves a lot of bits, but changes colors at edges and at severe transitions ftom one color to another neighboring color. I recommend this only for extremely high resolution images, where the effects will be much less noticeable, or where color accuracy isn't important, and only when filesize matters big-time. It can be effective with the aforementioned super-high resolution camera photos at full-size, though.)

Every image may need a tailored approach, but this is what I learned when working on a particular image I needed at three sizes. There are other, sometimes sneaky ways to save filesize, so try some things if you're adventurous, and see what works.

DeeDeeG avatar May 31 '17 06:05 DeeDeeG

Great question and answers @DeeDeeG. In your last comment you mentioned about cropping images before passing to guetzli. I'm interested to know how to do this correctly for JPEGs using Python. Because whichever tool I use (skimage, cv2 etc), they change the quality of the image on save? Do you recommend saving the cropped image(s) from those tools with quality=100 and then pass that to guetzli? Which library do you recommend for cropping?

Nithanaroy avatar Jul 23 '19 16:07 Nithanaroy

@Nithanaroy Hmm... I cropped/scaled manually with GIMP. (I am not much of a programmer myself, so I can't help much with Python specifically.)

I searched this in google: lossless crop python jpeg and the Python Imaging Library (PIL) is mentioned here, and supposedly can do a lossless crop. https://stackoverflow.com/questions/2933084/is-cropping-of-jpeg-images-using-the-pil-lossless (There is a currently maintained fork of PIL known as Pillow, apparently.)

You might also consider jpegtran which I believe is in c, but is probably the tool most focused on lossless operations of anything to do with jpeg. https://unix.stackexchange.com/questions/273097/jpegtran-losslessly-crop-left-portion-of-image https://en.wikipedia.org/wiki/Libjpeg#jpegtran

I think ImageMagick also tries to crop losslessly... https://en.wikipedia.org/wiki/ImageMagick#Other


That said, in general if you don't trust your tools to save losslessly, and you want to do lossless operations, I think working with PNG (or at least exporting/saving only as PNG) for lossless steps is the safest bet. Saving as PNG, in and of itself, should always be visually lossless. Any JPEG encode, you need to read up on your tool and confirm it is lossless from its documentation, I suppose.

Likewise, if you work in a non-destructive GUI editor (i.e. doesn't mess with your source file on disk unless you re-export there) like Photoshop or GIMP, you can modify the data multiple times, and export only once at the end in the desired quality. And you can always save as PNG (lossless) from these editors. That's what I did.

I believe encoding/saving JPEGS with q100 is generally NOT done losslessly. As this StackOverflow answer says, q100 and lossless are two different things. https://stackoverflow.com/questions/7982409/is-jpeg-lossless-when-quality-is-set-to-100

DeeDeeG avatar Jul 23 '19 17:07 DeeDeeG

A simple request.

Guetzli works great when I run it from cmd. For some reason, I can’t make it work when I use createprocess. It always returns 1. I can call mozjpeg using createprocess. My request is simple. If there’s something wrong with how you call guetzli.exe, then different return codes should be used.

Thank you for creating this great tool, Fredrik Wahlgren

Frwah avatar Oct 02 '19 23:10 Frwah

I found the solution to my problem. If you use CreateProcess, if the cmd string starts with a space character then it works. I used a debug build version, maybe the behavior is different when you build a release version.

Frwah avatar Oct 03 '19 22:10 Frwah

BTW. I have used JPEGmini which works well. It does have a nice feature which would be neat. It creates a comment so that it won’t process the same file more than once.

Frwah avatar Oct 03 '19 22:10 Frwah