Lossless JPEG optimization with FreeImage

There are several tools for optimizing JPEG without sacrificing quality, for example jpegtran.exe and Smush.it . I was looking for a way to do this using code (preferably in .NET), and currently I have settled on FreeImage , but I am not getting the result that I want.

There is a JPEG_OPTIMIZE flag, but you must also set the quality flag, and then it will not be lost.

This is what I tried:

 var image = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_JPEG, fileIn, FREE_IMAGE_LOAD_FLAGS.JPEG_ACCURATE); FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, image, fileOut, FREE_IMAGE_SAVE_FLAGS.JPEG_OPTIMIZE); 

But it does the compression. I thought FreeImage could optimize (by deleting metadata, etc.), but I cannot find how to maintain the same compression / image quality. What is the right way to do this?

Edit: There is some confusion that you cannot optimize JPEGs, the short answer is yes, you can, since you can strip out unused metadata. I'm not talking about compression. See these topics or check out Michael B.'s answer

Library for further lossless jpeg compression

Is Smush.it for Windows available?

Image compression tool

Image optimizer for images used on the Internet (jpg, gif and png)

Question: can this be done using FreeImage, and if so: how?

+6
source share
4 answers

I think that FreeImage only supports lossless transforms (i.e. FreeImage_JPEGTransform ).

This can be saved for different files, but, unfortunately, there is no way to set the save flags in a new file to allow the removal of metadata, etc.

I can only suggest you take a look at the source to find out if there is anything you can use on your own.

+2
source

I see that people mostly think about compression when they mention optimization ...

The program you mentioned (jpegtran) can decompress and recompress lossless jpeg images without loss and without compression. That is why you do not weaken quality. What can you do to optimize jpeg:

  • optimize the encoding level of the Huffman JPEG file to increase compression,
  • convert between progressive and progressive jpeg formats,
  • eliminate non-standard application-specific data inserted by some image programs
  • you can also apply shades of gray, rotate or crop without losing quality, but I think you're not interested in that.

Source: wikipedia.

I never did this, so I donโ€™t have anything by hand, but Iโ€™m quite sure that there is a library that can do this for you. Otherwise, creating something on your own should not be so difficult.

+2
source

I'm afraid JPEG is a lossy format. Even on a tiny scale. The quality flag tells him where on the scale you want your loss, the more losses, the better the compression, the less losses, the larger the file.

  • JPEG/2000 can perform lossless formats, but is not entirely supported.
  • PNG supports lossless compression.

However, you can optimize the JPEG file, but it will still lead to data loss, it can be meaningless data (i.e. EXIF โ€‹โ€‹information), however it will lose some of them, the size increase is minimal (if your file is already quite small). Here you will find here for a guide to delete EXIF โ€‹โ€‹data.

If you are trying to compress every last drop of size from a file, you can better switch to the "best" format (and I use this term freely, as what determines the best or worst format is your own requirements). But there are other formats that support higher compression with less loss.

+1
source

You can delete metadata using FreeImage, but for jpeg the image will be recompressed when saved with a corresponding loss in image quality.

 ' by default, FreeImage will have copied the metadata to the new image If Not (args.CopyMetadata) Then Dim tag As New FreeImageAPI.FITAG FreeImage.SetMetadata(FREE_IMAGE_MDMODEL.FIMD_COMMENTS, dib2, Nothing, tag) FreeImage.SetMetadata(FREE_IMAGE_MDMODEL.FIMD_IPTC, dib2, Nothing, tag) FreeImage.SetMetadata(FREE_IMAGE_MDMODEL.FIMD_XMP, dib2, Nothing, tag) ' value of 11 is for FIMD_EXIF_RAW FreeImage.SetMetadata(DirectCast(11, FreeImageAPI.FREE_IMAGE_MDMODEL), dib2, Nothing, tag) FreeImage.DeleteTag(tag) End If If File.Exists(targetFile) Then File.Delete(targetFile) End If FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib2, targetFile, FREE_IMAGE_SAVE_FLAGS.JPEG_OPTIMIZE Or FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYGOOD) 

If you want to remove metadata from file types other than FIF_JPEG, refer to the documentation to find out which ones can be written.

To remove metadata without affecting the image, you will need to view the jpeg file exchange format and pull out only the data you need. If you have the Photoshop 6 installation CD (6, not CS6) installed, a document with the appropriate information is available when installing the PS6 SDK.

+1
source

Source: https://habr.com/ru/post/920083/


All Articles