Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Estimate the filesize before thumbnailing an image? #9149

Answered by radarhere
jrief asked this question in Q&A
Discussion options

Is there a way to roughly estimate the filesize before thumbnailing an image?

I did some tests and hoped that the file size would be roughly proportional to the sum of pixels (area) in the thumbnailed image, but this is not the case. It seems as if smaller images proportionally require more disc space than larger images.

To show this effect, I used a JPEG photo sized 1920x1080 with a filesize of 245592 bytes. From this photo I created multiple thumbnails keeping the same aspect ratio. These are my results:

  • Thumbnail to 306x172. Estimated size: 6233. Real size: 13785
  • Thumbnail to 368x207. Estimated size: 9022. Real size: 18218
  • Thumbnail to 442x249. Estimated size: 13034. Real size: 24713
  • Thumbnail to 531x299. Estimated size: 18804. Real size: 33306
  • Thumbnail to 638x359. Estimated size: 27127. Real size: 43697
  • Thumbnail to 766x431. Estimated size: 39101. Real size: 58342
  • Thumbnail to 921x518. Estimated size: 56503. Real size: 77672
  • Thumbnail to 1107x623. Estimated size: 81681. Real size: 102764
  • Thumbnail to 1330x748. Estimated size: 117826. Real size: 136657
  • Thumbnail to 1598x899. Estimated size: 170147. Real size: 180594

For estimating the file size I use this formula:

(thumb_width * thumb_height) / (orig_width * orig_height) * orig_file_size

Is there a kind of correction factor to get better estimates? Presumably one has to take the image's entropy into account, since that may vary widly. Maybe I have to use PIL's Image.histogram or another related function. But I have no idea where to start from.

You must be logged in to vote

If you're willing to create a larger thumbnail in the process of generating this estimate, then why not actually create a thumbnail at the intended size and see what filesize it is? It would be quicker.

Replies: 1 comment 2 replies

Comment options

I'm not very optimistic. What level of accuracy are you hoping for in these estimates?

  • I don't know if you're only talking about JPEG images, or other formats as well
  • I expect this to work differently for different image modes
  • Are you specifying resample or reducing_gap when using thumbnail()? Are you passing any additional arguments to save()?
  • When you say orig_file_size, do you mean you're hoping to calculate the size of Pillow's saved image based on the size of the image you opened? There's no guarantee that Pillow uses the same compression method as an image generated by another piece of software, and may actually increase the file size (JPEG format image not getting compressed #2238 )
  • Are you hoping this estimate will be valid across different Pillow installations? It may depend on how Pillow was built, or what version is in use. For example, zlib vs zlib-ng can change the filesize of PNG images (Use zlib-ng instead of zlib #8500 )
  • I'm surprised you are willing to use histogram(). I would have thought of that as an expensive operation, and I expected the point of your question was that you wanted to estimate the file size very quickly.
You must be logged in to vote
2 replies
Comment options

Hmm, doesn't sound so good.
I was thinking about creating one thumbnail in the highest and one in the lowest resolution. Then by looking at some other parameters (histogram, entropy, etc.), for both of these two images, there might be an additional interpolation parameter to roughly estimate the size of an image between those two. +/-1kB would be a perfectly fine estimate.

Comment options

If you're willing to create a larger thumbnail in the process of generating this estimate, then why not actually create a thumbnail at the intended size and see what filesize it is? It would be quicker.

Answer selected by radarhere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /