Converting image with Base64 encode will give bigger results size


Base64 usually needed to represent binary data into ASCII format, or in other case to make it more “readable”. Some case study why we need to convert binary into ASCII like this :

1. There is Android App that have function to take picture
2. This picture will send to website
3. In Website (Django eg) it will have method to handle the picture

When we using POST binary data, there will issue like CSRF_TOKEN, filesize, resume feature, etc. It will hard thing to track the percentage of binary data by the size. Using base64 by encode the image will be a good solution to tackle this problem. Instead of using POST data, we can use XML-RPC and send the “string” of the photos. Later on we can decode back into binary data.

On interesting fact here that base64 encode results will be 1.37 bigger than the actual binary data.
Here is to do in python:

1
2
3
4
5
6
7
8
9
import base64

f = open("grimace.jpg")
encoded = base64.b64encode(f.read())
f.close()

a = open("grimace.txt", "w+")
a.write(encoded)
a.close()

And the results by using “ls” :

1
2
26545 grimace.jpg
35396 grimace.txt

Why it’s get bigger? Because we represent the binary (01111) into ASCII which it take 8 bit and plus with the headers of the files.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.