apoorv-2204
Is lossless image and PDF compression possible?
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
Most Liked
benwilson512
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.
kip
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.
kip
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?
mindok
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.
harrisi
You got PNG and JPEG mixed up here - JPEG is (typically) lossy, PNG is lossless. Good info, though!







