Hi ππ
In this post i will share with you how to convert image to base64 using Python
step 1
You need image for test
step 2
Read the image using rb as the reading mode to read bytes from the image and encode them.
import base64
with open('alone.jpg', 'rb') as img:
base64_bytes = base64.b64encode(img.read())
Now we have base64_bytes
we need to decode this using decode function
base64_str = base64_bytes.decode()
f = open('base64_str.txt', 'a+')
f.write(base64_str)
f.close()
Now that we have a base64 string in the text file base64_str.txt
, we can use this to display an image from the base64 string by copying all of base64_str.txt
and using sites like codebeautify.
https://codebeautify.org/base64-to-image-converter
Result
Now we're done π€
Don't forget to like and follow π
Support me on PayPal π€
https://www.paypal.com/paypalme/amr396
Top comments (0)