3

So I have some Binary data in python (a jpg Image, in this case) that I am retreiving from an API as a B64 encoded string. Is there an easy way to estimate the size of this (image) file on the disk?

<Image>/9j/4AAQSkZJRgABAgEASABIAAD/4RriRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA
AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAeAAAAcgEyAAIAAAAUAAAAkIdp
AAQAAAABAAAApAAAANAACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9z ....... </Image>

2 Answers 2

18

You can multiply the length of the string by 3/4 to get the size in bytes.

(len(str) * 3) / 4
Sign up to request clarification or add additional context in comments.

2 Comments

isn't Base64 larger than the actual size in bytes? I always thought that size of B64 > Size in Bytes, whereas in this case it would be the opposite
As a follow up question, is the size of the Base64 string itself == the length of the string? I know that its true for 8bit strings, which are the default string encoding in python
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.