Get Image File Size From Base64 String

前端 未结 3 1937
青春惊慌失措
青春惊慌失措 2020-12-19 00:47

I\'m working on a python web service. It calls another web service to change the picture of a profile.

It connects to another web service. This web service can only

相关标签:
3条回答
  • 2020-12-19 01:17

    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 no, one or two characters =, as explained here.

    Probably not optimal. I don't how efficient str.count(char) is. On the other hand, it is only performed on a string of length 2.

    0 讨论(0)
  • 2020-12-19 01:20

    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.

    0 讨论(0)
  • 2020-12-19 01:28

    image size can be calculated from the string length. Since every character represents 6 bits and every '=' sign shows lack of 8 bits. ref

    file_size = (len(base64_string) * 6 - base64_string.count('=') * 8) / 8
    

    or

    file_size = len(base64_string) * 3 / 4 - base64_string.count('=')
    

    will give the file size in byte and, dividing it in mega(=1000000) will give the file size in megabyte

    0 讨论(0)
提交回复
热议问题