Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for libwebp 'target_size' parameter #3824

Closed
jcupitt opened this issue Jan 21, 2024 Discussed in #3822 · 6 comments · Fixed by #3950
Closed

Add support for libwebp 'target_size' parameter #3824

jcupitt opened this issue Jan 21, 2024 Discussed in #3822 · 6 comments · Fixed by #3950

Comments

@jcupitt
Copy link
Member

jcupitt commented Jan 21, 2024

Discussed in #3822

Originally posted by john-parton January 20, 2024
The webp API supports saving with a "target_size" parameter to limit the maximum size of the resulting file.

int target_size;        // if non-zero, set the desired target size in bytes.
                        // Takes precedence over the 'compression' parameter.

https://developers.google.com/speed/webp/docs/api

As far as I can tell, this isn't exposed in libvips (and consequently pyvips, the binding I am using.)

This is a useful feature for my use case. I preprocess images before they are sent to CloudFlare Images. They have specific limits for resolution, but also an overall limit for file size. In my case, I set images to be at most 6,000 x 6,000 pixels and am saving with Q=90. For the vast majority of images, that's fine, because the resulting image is usually significantly less than 10MB, but for some noisy images with difficult to compress features, it just barely goes over the 10MB boundary.

If I could just pass this in, I wouldn't have to worry about, but now I'm faced with having to either:

  1. Lower my resolution and Q factor for all images
  2. Check the resulting output size every time, and then adjust either the resolution or Q factor if it's in excess of 10MB.

I wouldn't really consider either of these to be a good solution.

@john-parton
Copy link
Contributor

I can try and get an MVP for this change. It doesn't look too hard, but my C/C++ is pretty bad. Will probably need a bit of help pushing the final bit through.

@jcupitt
Copy link
Member Author

jcupitt commented Jan 22, 2024

Sure, happy to help.

@john-parton
Copy link
Contributor

I created a pull request. Turns out to work just fine once you realize that the target_size feature requires pass to be greater than 1.

@john-parton
Copy link
Contributor

@jcupitt Don't want to be a nuisance, but did you see this? I'd love to hear your opinion.

@jcupitt
Copy link
Member Author

jcupitt commented May 3, 2024

Sorry @john-parton, I reviewed now.

@john-parton
Copy link
Contributor

Thanks so much! I'll continue our discussion over there.

No need to apologize; I'm just excited to get something going!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants