How to make jpeg images smaller without visual difference

ever since we moved to use Aspose.Imaging our image sizes increased by at least x2 or x3. I am comparing this with images that were saved before using mozjpeg optimization. a 360KB images saved using the SDK today can be squashed using tinyjpg.com to 79KB(!) and you cannot see the difference. How can we save on disk space? We have millions of images which cannot be sent to an online service like tinyjpg.com and I need them to be smaller while keeping quality as good as possible. It feels like Aspose.Imaging is using the .net jpeg encoder which is far from optimized. Even a pass though mozjpeg would probably help the jpeg size but an algorithm like tinyjpg has would go a long way.

any recommendation of algorithm? SDK? command line tool? something else?

@DanAvni

To reduce the size of JPEG images while maintaining visual quality using Aspose.Imaging, you can adjust the compression quality when saving the images. The default settings may not be optimized for size reduction. Here’s a simple example of how to save a JPEG image with a specified quality level:

using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;

// Load the image
using (Image image = Image.Load("input.jpg"))
{
    // Create an instance of JpegOptions
    JpegOptions jpegOptions = new JpegOptions
    {
        Quality = 75 // Adjust the quality (0-100) to reduce file size
    };

    // Save the image with the specified options
    image.Save("output.jpg", jpegOptions);
}

You can experiment with the Quality property to find a balance between file size and image quality. A lower quality value will result in a smaller file size but may introduce visible artifacts.

If you find that Aspose.Imaging does not meet your compression needs, consider using external libraries or command-line tools that specialize in image optimization. These tools often provide advanced algorithms for reducing file sizes without significant quality loss.

Feel free to reach out if you have further questions!

using quality is not optimal because the image contents might be a document or a landscape photo, an arbirtary quality of 75 might make the text unreadable but might work for landscape

@DanAvni , we will review your request. You will be answered shortly!

@DanAvni Perhaps the YCbCr color scheme will suit you.

using (var image = Image.Load(@"image.jpg"))
{
    image.Save(@"out.jpg", new JpegOptions(){ColorType = JpegCompressionColorMode.YCbCr });
}

Hello, @DanAvni ,
I have tested a few Imaging tools for a set of pictures and found that optimization gap is not always significant. When compressing the source image to Jpeg you should use YCbCr colorspace with Progressive or Baseline options, since it often produces the best compression ratio:

new JpegOptions()
// or
new JpegOptions()
{
    ColorType = JpegCompressionColorMode.YCbCr,
    CompressionType = JpegCompressionMode.Progressive,
}

In case you already use such options, you might consider storing the images as WEBP for higher compression ratio without visual difference close to optimized Jpeg.

var inputPath = @"input.jpeg";
using (var image = Image.Load(inputPath))
{
    image.Save(inputPath + ".webp", new WebPOptions());
}

Hope this helps!

Hi @stanislav.popov

I am already using YCbCr and Baseline. Progressive produces yellow artifacts on images as I have shown in a previous post I made a few months ago. Changing to WebP is not an option because my jpeg images are sent to customers who expect to receive jpeg and it will not be an easy change to move all customers. Moreover, you cannot ignore the fact that tinyjpg and other online compression sites can take an image an still keep it a jpeg without any visual loss but the image is about 1/5 of the original (See attached image I saved with Aspose.Imaging and try to pass that through tinyjpg)
1_o_w.zip (350.7 KB)

@DanAvni I tested reduced the size of this image using tinyjpg. The result isn’t 100% quality, but rather about 55-60.
If you set this quality in Aspose, you will get an approximate result
For clarity, I’ve added three windows, with the results arranged from left to right: Original (aspose 100%), Tinyjpg, and Aspose 58%.
As you can see, the left image has better quality, while the two on the right are roughly the same quality and size.
example.jpg (88.4 KB)

If you have other examples, please send them to us so we can examine them.

The trick is that they do this automatically. They determine the best quality to save so that it will not be visually noticable. I really don’t care about the way but I care about the end result: smallest files with best quality possible

@DanAvni, Currently, our library doesn’t have an auto-quality feature for JPEGs.
I’ll open a task on this issue.
For now, I can recommend three options:

  1. Use 75% quality; this will almost always provide a visible quality and a size close to what Tinyjpeg produces.
  2. Perform an auto-quality manually using the Aspose.Imaging library, cyclically comparing the result with the original using the PSNR method.
  3. Implement storage in webp format and on-the-fly conversion to JPEG upon request.

@DanAvni
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): IMAGINGNET-7808

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.