Python 图片与字符串互转

发布时间:2019-06-21 17:33:18编辑:auto阅读(2615)

    #coding:utf-8
    
    # 引入base64库
    import base64
    
    # 把图片压缩成字符串
    with open("c:\\test.jpg", "rb") as imageFile:
        image2str = base64.b64encode(imageFile.read())
        #print image2str
        
    # 把字符串还原成图片
    with open("c:\\str2image.png",'wb') as str2image:
        str2image.write(image2str.decode('base64'))


关键字