zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python String - Image 相互转换

Python转换 string Image 相互
2023-09-14 09:07:03 时间

Image2String

import base64
def image_str():
    with open("test.png", "rb") as imageFile:
        str = base64.b64encode(imageFile.read())
        return str

String2Image

def str_image(str):
    fh = open("o_test.png", "wb")
    fh.write(str.decode('base64'))
    fh.close()

示例

import base64
import sys as sys
def image_str():
    with open("test.png", "rb") as imageFile:
        str = base64.b64encode(imageFile.read())
        return str

def str_image(str):
    fh = open("o_test.png", "wb")
    fh.write(str.decode('base64'))
    fh.close()

str = image_str()
print str
print sys.getsizeof(str)
str_image(str)