Ability to provide a target file size
DreamingInBinary opened this issue · 2 comments
Apologies if I skipped over this, but does this library support resizing images to a target byte size? For our use case, we're trying to ensure any image we use is under 10 megabytes. I do see that spectrum supports specific size dimensions but as far as I can tell, not byte size.
Thanks!
Hi @DreamingInBinary, that observation is correct: currently there is no way to provide a target size. I'll answer a bit more verbose - just for future documentation. I also took the freedom to rephrase the issue title.
Not having the "target size" option has many reasons: one is that the resulting size of the encoded stream depends on many factors including the input image (e.g. its content), the target resolution, and the compressor parameters (e.g. image format, quality value, chroma subsampling).
Therefore, it would require multiple rounds (and e.g. binary search it we only look at one parameter) to find a good configuration. On mobile platform this is something we'd like to avoid as it "wastes" a lot of battery.
For your use case, I can suggest that you pick a configuration where you have a decent safety margin. E.g. restricting the output resolution to 4096x4096 and having a JPEG quality of 85 should almost never exceed 10 MiB. If it occasionally does (e.g. adversarial input), then cut the number of output pixels in half.
Perfect, thanks for the all the context @lambdapioneer !