We have a service in production where we accept documents from users/clients
mainly: jpeg, png and pdf. And there are several of this file provided by client to fulfill the requirements.
The issues is when these files are needed to be send over email. with email size limit of 25 mb , the mail fails as of SendGrid services
I want to compress the incoming images png jpeg and pdf
But I don't want to loss quality, and they should be at least legible
What should I use what lib?
anything existing in elixir eco
Images, including jpeg and png, already contain compressed data so I would be surprised if they will compress significantly to get you under the 25mb limit if you’re already over it. I typically find a compressed jpeg/png image is only about 10% smaller than the original image.
Your original message says “lossless”. Higher jpeg/png compression (ie lower quality) is not lossless. Can you be a bit more specific about your requirements?
I simply searched for “optimize image api” and drowned in results.
As for lossless / lossy, IMO any image that’s 25MB is way too big. Though I don’t know your scenario, maybe you are working with galactic deep-field assembled images.
As @kip has mentioned, JPG compression is lossless.
You can experiment with image editors or command line tools to get a sense of what loss you can get away with without breaking the use case, and it highly depends on the type of image. You typically have two variables to play with - image dimensions (i.e. resampling the image to make it smaller - depending on the types of image, some interpolation algorithms can be better than others), and a “quality” parameter. Due to the way JPG compression works, you can generally compress more when you have smooth gradients (e.g. skin tones with shadows), whereas lots of sharp colour changes (e.g. screenshots) tend to look terrible when heavily compressed.
PNG compression can be lossless - it works really well for screenshots where you have big blocks of the same colour. Generally it’s a tradeoff between computation time and compression.
PDF compression is complex - I’d set aside a good week for research and testing if you are going down that path. It can involve things like font stripping, embedded image compression etc, etc. I’ve gone down that rabbit hole and there are no easy answers. There are PDF compression services that can help, but I found nothing I would be happy to embed in my stack.
You’ve got some solid answers in the compression front so I’ll do the other angle: see if you can change the requirement to allow links to files. There’s a reason companies that specialize in document sharing don’t email them, and instead do links.
It should be the other way round. Our eye is much more sensitive for brightness than color changes. If the compression algorithm has to remove information in a gradient we tend to see it. A solid block of color in the other hand is easy to compress because it’s mostly a redundant information. With the contrast changes you are right because the compression algorithm reduces the “frequency” with something called a Discrete Cosine Transform (DCT). But there are more factors in the compression like color subsampling etc
I’ve updated the title though as pointed out by others it is not clear what you are after.
If you don’t want to lose quality then your best bet may be to zip the file and send that - this will not lose quality of the original file. If you compress image files themselves then, as pointed out already, that is lossy compression - however you can use tricks like converting PNGs to jpeg, reducing image size etc.
Also keep in mind many email providers have a limit on incoming email size as well.