There are several tools to optimize a JPEG without sacrificing quality, such as jpegtran.exe and Smush.it. I've been looking for a way to do it through code (preferably in .NET) and I'm currently settled on FreeImage but I'm not getting the result I want.
There's an JPEG_OPTIMIZE
flag but you have to set a quality flag as well and then it's not lossless anymore.
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 a compression. I thought FreeImage could optimize (by stripping out metadata etc) but I can't find how to keep the same compression/image quality. What's the proper way to do it?
Edit: There's some confusion about not being able to optimize JPEGs, short answer is yes you can since you can strip out unused metadata. I'm not talking about compression. See these threads or check Michael B's answer.
Library for further lossless jpeg compression
Is there a Windows version of Smush.it available?
Tool for Image compression
Image Optimizer for images used on the web (jpg, gif and png)
Question is: Can it be done with FreeImage, and if so: how?
I think FreeImage only supports lossless transformations (ie FreeImage_JPEGTransform
).
This can save to different files, but unfortunately there doesn't appear to be a way to set any save flags on the new file to allow stripping of metadata etc.
I can only suggest you have a look at the source to see if there is anything you can utilise yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With