-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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
resampleorreducing_gapwhen usingthumbnail()? Are you passing any additional arguments tosave()? - 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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.