浏览文章
文章信息
最新Python3.8 图片转Base64编码
14035
注意编码后要转str.
bytes.decode(base64_str) # 转str
代码:
with open(file_path, 'rb') as f1: base64_bytes= base64.b64encode(f1.read()) # base64类型 # b'JVBERi0xLjUNCiXi48 src = bytes.decode(base64_bytes) # 转str # 猜测文件类型 # JVBERi0xLjUNCiXi48/ # data = flask.request.host_url+file_path.replace(UPLOAD_FOLDER, 'files').replace('\\', '/') base_header_str = "data:image/"+os.path.splitext(file_path)[1]+";base64," data = base_header_str+src