Jpeg compression

Are there any options in the library to change the compression to be similar to what GitHub - mozilla/mozjpeg: Improved JPEG encoder. does? I have achieved some great compressions with it and trying the same with

ColorType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode.YCbCr,
CompressionType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode.Baseline,
//4:2:0 compression produces the smallest file size. the values below are for 4:2:0
HorizontalSampling = new byte[] { 2, 1, 1 },
VerticalSampling = new byte[] { 2, 1, 1 }

produces a larger file than what the mozjpeg library produced in my tests over the same source image saved with the same quality

@DanAvni

To achieve JPEG compression similar to what the mozjpeg library offers, you can adjust the quality settings and compression modes in Aspose.Imaging. By default, JPEG images are saved with a quality setting of 75, which can be increased to improve compression results.

Here’s an example of how to set the quality and compression type in Aspose.Imaging:

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.ImageOptions;

public class JpegCompressionExample
{
    public static void Main()
    {
        // Load the image
        using (Image image = Image.Load("source_image.jpeg"))
        {
            // Create JPEG options
            JpegOptions jpegOptions = new JpegOptions
            {
                CompressionType = JpegCompressionMode.Baseline, // or JpegCompressionMode.Progressive
                Quality = 95 // Increase quality for better compression
            };

            // Save the image with the specified options
            image.Save("compressed_image.jpeg", jpegOptions);
        }
    }
}

In this example, the quality is set to 95, which should help reduce the file size while maintaining image quality. You can experiment with different quality values to find the best balance for your needs.

Additionally, if you are using 4:2:0 chroma subsampling, you can specify the horizontal and vertical sampling factors as follows:

jpegOptions.HorizontalSampling = new byte[] { 2, 1, 1 };
jpegOptions.VerticalSampling = new byte[] { 2, 1, 1 };

This configuration can help achieve smaller file sizes similar to those produced by mozjpeg. If you continue to experience larger file sizes, consider adjusting the quality further or experimenting with different compression modes available in the library.

For more detailed examples and options, you can refer to the Aspose.Imaging documentation and sample code available on their GitHub repository.

Sources:
[1]: JPEG/JPG has less size after it’s stamped
[2]: OptimizationStrategyInJPEG.java

@DanAvni
It is possible. The different jpeg libraries provide different compression quality and speed.
According to HorizontalSampling and VerticalSampling, please be aware they are used only if CompressionType == Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode.JpegLs

I need to stay within the realms of jpeg and not jpeg-ls because the image is sent to customers who might not support other formats. what are my options for creating the smallest jpg like the mozjpeg library does?

@evgeniy.sidenko , also, trying to use progressinve compression with quality 95 on the attached png
test.png (80.1 KB)

trying to save it to jpg introduces a lot of yellow artifacts around the bottom right. On other painting tools I have, I see the artifacts starting at quality of about 60-70 and definitly not at 95. can you please share some insight into it?

trying to save it to jpg introduces a lot of yellow artifacts around the bottom right.

I will take a look at this.

I see the artifacts starting at quality of about 60-70 and definitly not at 95

Yes, it is possible, as JPEG is a lossy image format. With the decrease in quality, the JPEG compression algorithm produces more artifacts. If it is not appropriate for you, perhaps you should choose another image format.

@DanAvni
We confirm it is a bug.

Trying to save it to a JPEG introduces a lot of yellow artifacts around the bottom right.

@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-7609

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.