Python 保存 Base64 图片到硬盘

Posted by wxianfeng Fri, 14 Feb 2014 12:53:00 GMT

服务端收到 base64 编码的图片,需要保存到硬盘。

注意需要先把 base64 字符串转为字节,然后使用 base64 的 decodebytes 解出。

核心代码:

with open(tmp_path, "wb") as fh:
    fh.write(base64.decodebytes(base64_str.encode()))

字符串和字节互转代码

1. To convert a string to bytes.
data = ""                    #string
data = "".encode()        #bytes
data = b""                     #bytes


2. To convert bytes to a String.
data = b""            #bytes
data = b"".decode() #string
data = str(b"")         #string

This entry was posted on Fri, 14 Feb 2014 12:53:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags ,


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=322

Comments

Leave a comment