Python base64 string to PyPng without saving file

三世轮回 提交于 2019-12-25 03:07:33

问题


How do you do this without saving to disk and then opening 'out.png' ?

    r = requests.get(url)
    mine,encoded =  r.json()[0]['data'].split(',') #if it is segmentation
    decoded = base64.decodestring(encoded)

    if mine == 'data:image/png;base64':
        #TODO do this from memory
        g = open("out.png", "w")
        g.write(decoded)
        g.close()            

        r = png.Reader('out.png')
        print r.read()

回答1:


Use the bytes keyword

r = png.Reader(bytes=decoded)



回答2:


base53.decodestring() returns a string of the binary data, and according to this page png.Reader() can take a string as input, so you just have to link the two together.



来源:https://stackoverflow.com/questions/27218529/python-base64-string-to-pypng-without-saving-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!