python String - Image 相互轉換


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)
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM