Beginners Base64 Code, make a note
First, write a simple example:
Encodes a string and then decodes it, this example comes from the https://docs.python.org/2/library/base64.html
>>> Import base64
>>> encoded = base64.b64encode (' Data to be encoded ')
>>> encoded< c2/> ' ZGF0YSB0BYBIZSBLBMNVZGVK '
>>> data = base64.b64decode (encoded)
>>> data
' data to Be encoded '
Requirements: Converts a picture file into a base64 encoding, and then decodes
#import os
import base64
#这里我使用spyder IDE, the pictures are stored under the Pythondemo folder
#查看当前路径,
# OS.GETCWD ()
# Modify the path to the directory where the picture is located
#os. ChDir (' Xxx/xx/pythondemo ')
#打开文件, picture name 11.jpg, read
r_data = open (' 11.jpg ', ' RB ') in binary form )
data_string = R_data.read ()
# with base64 encoding
encode = base64.encodestring (data_string)
encoded2 = Base64.b64encode (data_string)
# Print encode
# decoding
decode = base64.decodestring (encode)
Decoded2 = Base64.b64decode (encoded2)
#写入文件
w_data = open (' 11w.dat ', ' WB ')
W_data.write (decode)
W_data2 = Open (' 112.dat ', ' WB ')
W_data2.write (DECODED2)