Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Image File Size From Base64 String

Tags:

python

image

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 accept pictures that are 4 MB or smaller.

I will put the checking in the first web service. It uses PIL to check if the base64 string is a valid image. However, how do I check if the base64 string will create a 4 MB or smaller image?

like image 860
Jon Avatar asked Aug 01 '12 14:08

Jon


People also ask

How does Base64 determine file size?

Very roughly, the final size of Base64-encoded binary data is equal to 1.37 times the original data size + 814 bytes (for headers). You can use ContentLength property of the request to determine what the size is in bytes, although if you are uploading more then one image, it might be trickier.

What is the size of Base64 image?

more than 2500x2500 pixels. 4-8kb in size.

How many bytes is a Base64 string?

Length of data Base64 uses 4 ascii characters to encode 24-bits (3 bytes) of data.

Does Base64 reduce size?

File size. Image Base64 encoding is not the most efficient way to encode data when it comes to filing size. This is because the process always results in a 20%-25% increase in file size at least. For example, if you have a binary file that is 1000 bytes in size, after Base64 encoding, it will be 1250 bytes in size.


1 Answers

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.

like image 176
Ignacio Vazquez-Abrams Avatar answered Sep 30 '22 17:09

Ignacio Vazquez-Abrams