sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Adjust quality based on output file size

Open danvoyce opened this issue 2 months ago • 2 comments

Feature request

I know this feature has been requested a lot over the years, but we are in the future now so I'd like to think there's a way to do this that's smarter than the brute force approach.

What are you trying to achieve?

Ultimately what I and I believe others are wanting to achieve is specific output size, so no matter what size file we are trying to compress, we can be sure that it's below x.

Yes we can brute force it, but that's not very performant.

I'd like to think with someone a lot smarter than me could potentially use some ML magic and come up with a better approach...

Anyone?

danvoyce avatar Apr 18 '24 16:04 danvoyce

The performance of the "brute force" example you've linked to could be improved, or at least made more consistent, with the use of binary search.

Given most lossy image encoding is entropy-based, you may also have some luck by investigating either linear or polynomial regression ("AI" from the 19th century) of image entropy for a given image corpus that represents your scenario. For JPEG encoding, the higher the entropy value of an image, the lower the required quality value is likely to be in order to hit a target file size.

lovell avatar Apr 18 '24 19:04 lovell

Is this something you'd consider adding to the library? I'd love to dig deeper on this but don't have the time at the mo or know where to start 😅

danvoyce avatar Apr 20 '24 08:04 danvoyce