Encoding Binary Files in Python Using Base64

Posted by

Base64 Encoding Binary Files in Python

Base64 Encoding Binary Files in Python

Base64 encoding is a process of converting binary data into a ASCII string format. This is useful when transferring binary files such as images or documents over text-based protocols like HTTP.

In Python, you can use the base64 module to perform base64 encoding of binary files. Here’s an example of how to do this:

import base64

with open('file.jpg', 'rb') as binary_file:
    binary_data = binary_file.read()
    base64_encoded_data = base64.b64encode(binary_data)
    base64_string = base64_encoded_data.decode('utf-8')

print(base64_string)
	

In this example, we opened a binary file ‘file.jpg’ in read (‘rb’) mode and read its contents into binary_data variable. We then used the base64.b64encode() function to encode the binary data into base64 format. Finally, we decoded the base64 data into a UTF-8 string using the decode() method.

You can then use this base64 string to transmit the binary file as text data over a network or store it in a database.

Base64 encoding is also useful for embedding binary data in HTML, CSS, and JavaScript. For example, you can use base64-encoded images in HTML <img> tags like this:

<img src="data:image/jpeg;base64, {base64_string}" />
	

This will display the image without the need for a separate image file. Similarly, you can use base64-encoded binary data in CSS for background images, or in JavaScript for embedding files in web applications.

Base64 encoding is a simple and effective way to transfer and embed binary data in text-based formats. It’s widely supported and easy to use, making it a versatile tool for handling binary files in Python and web development.

0 0 votes
Article Rating
15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@skarbazalam4284
10 months ago

Hi, can you help me to generate the barcode from ".bin" binary file? I tried many methods but I failed, Please can you help me to solve this problem?

@yakupalienescapkan6297
10 months ago

what program are you using

@hulusewferede466
10 months ago

Can we use b64 for video files in different applications

@bmansan
10 months ago

JESUS CHRIST!
I almost went blind on 1:57
Was watching with the phone close to my face already in bed

@A.A.Selwy-77
10 months ago

Perfect.
on time.

@anantraj4664
10 months ago

bro some of my shortcut keys in pycharm clash with vim how do i fix that?

@paulthomas1052
10 months ago

Thanks – very useful tips.

@philtoa334
10 months ago

Thx_.

@heoheo4954
10 months ago

Tks 😊

@serychristianrenaud
10 months ago

thank

@omarabdul-hafiz6467
10 months ago

That's awesome!

Could you please do a tutorial on how to use Flask-Admin and restrict access to certain pages/parts of the website in Flask?

@proplayerhacker
10 months ago

i appreciate what you're doing for us, really i love your content ❤

@Ironpants57
10 months ago

That's pretty cool, I wonder what the size comparison is between input and output. I'll have to play around with this. Thank you for the video!

@JNET_Reloaded
10 months ago

well that was long and boring

@r5bc
10 months ago

1st comment and like before to watch