Get Image File Size From Base64 String
Multiply the length of the data by 3/4, since encoding turns 6 bytes into 8. If the result is within a few bytes of 4MB then you'll need to count the number of =
at the end.
I'm using this:
def size(b64string):
return (len(b64string) * 3) / 4 - b64string.count('=', -2)
We remove the length of the padding, which is either zero, one, or two characters =
, as explained here.
Probably not optimal. I don't know how efficient str.count(char)
is. On the other hand, it is only performed on a string of length 2.