Python3将html画布中的png数据写入文件

时间:2018-12-28 07:06:25

标签: python

我以前有一些代码是使用python 2.7编写并使用的,我想移植到python 3.6,该代码旨在使用Django从HTML画布读取的png-data

通过json传递过来的数据看起来像这样

{"png-data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhMAAAFQCAYAAAAFh/x7AAAS00lEQVR4Xu3dvY6tdRkF8AcVPy5AuAK9AUsxVKJ0Biw03IANDbdCraWVFmiDGhtotaIxgQtAozdgFM1rZgLCfO3z7M2sdfidhETD/Ldrfuv/7iznnDPzzPhFgAABAgQIEFgIPLM46ygBAgQIECBAYIwJl4AAAQIECBBYCRgTKz6HCRAgQIAAAWPCHSBAgAABAgRWAsbEis9hAgQIECBAwJhwBwgQIECAAIGVgDGx4nOYAAECBAgQMCbcAQIECBAgQGAlYEys BwmQIAAAQIEjAl3gAABAgQIEFgJGBMrPocJECBAgAABY8IdIECAAAECBFYCxsSKz2ECBAgQIEDAmHAHCBAgQIAAgZWAMbHic5gAAQIECBAwJtwBAgQIECBAYCVgTKz4HCZAgAABAgSMCXeAAAECBAgQWAkYEys hwkQIECAAAFjwh0gQIAAAQIEVgLGxIrPYQIECBAgQMCYcAcIECBAgACBlYAxseJzmAABAgQIEDAm3AECBAgQIEBgJWBMrPgcJkCAAAECBIwJd4AAAQIECBBYCRgTKz6HCRAgQIAAAWPCHSBAgAABAgRWAsbEis9hAgQIECBAwJhwBwgQIECAAIGVgDGx4nOYAAECBAgQMCbcAQIECBAgQGAlYEys BwmQIAAAQIEjAl3gAABAgQIEFgJGBMrPocJECBAgAABY8IdIECAAAECBFYCxsSKz2ECBAgQIEDAmHAHCBAgQIAAgZWAMbHic5gAAQIECBAwJtwBAgQIECBAYCVgTKz4HCZAgAABAgSMCXeAAAECBAgQWAkYEys hwkQIECAAAFjwh0gQIAAAQIEVgLGxIrPYQIECBAgQMCYcAcIECBAgACBlYAxseJzmAABAgQIEDAm3AECBAgQIEBgJWBMrPgcJkCAAAECBIwJd4AAAQI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/m ObMHF/VePNqfBwfegyJ12fm/eDPSzQCBAisBIyJFZ/DXzCBTw F60//HzPz05n5wxfMw6dLgACB/wkYEy4CgdMFj ...

然后在后端读取

png_data = data['png-data']

开始处理...

img_str = re.search(r'base64,(.*)', png_data).group(1)

然后尝试保存它

image = open(image_name, 'wb')
image.write(img_str.decode('base64'))

这会引发错误

'str' object has no attribute 'decode'

我以为它可能会自动解码,但尝试时不进行解码'

image.write(img_str)

引发错误

a bytes-like object is required, not 'str'

1 个答案:

答案 0 :(得分:1)

尝试一下:

import re
import base64

OUTPUT_FILE = 'result.png'
DATA = {"png-data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhMAAAFQCAYAAAAFh/x7AAAS00lEQVR4Xu3dvY6tdRkF8AcVPy5AuAK9AUsxVKJ0Biw03IANDbdCraWVFmiDGhtotaIxgQtAozdgFM1rZgLCfO3z7M2sdfidhETD/Ldrfuv/7iznnDPzzPhFgAABAgQIEFgIPLM46ygBAgQIECBAYIwJl4AAAQIECBBYCRgTKz6HCRAgQIAAAWPCHSBAgAABAgRWAsbEis9hAgQIECBAwJhwBwgQIECAAIGVgDGx4nOYAAECBAgQMCbcAQIECBAgQGAlYEys BwmQIAAAQIEjAl3gAABAgQIEFgJGBMrPocJECBAgAABY8IdIECAAAECBFYCxsSKz2ECBAgQIEDAmHAHCBAgQIAAgZWAMbHic5gAAQIECBAwJtwBAgQIECBAYCVgTKz4HCZAgAABAgSMCXeAAAECBAgQWAkYEys hwkQIECAAAFjwh0gQIAAAQIEVgLGxIrPYQIECBAgQMCYcAcIECBAgACBlYAxseJzmAABAgQIEDAm3AECBAgQIEBgJWBMrPgcJkCAAAECBIwJd4AAAQIECBBYCRgTKz6HCRAgQIAAAWPCHSBAgAABAgRWAsbEis9hAgQIECBAwJhwBwgQIECAAIGVgDGx4nOYAAECBAgQMCbcAQIECBAgQGAlYEys BwmQIAAAQIEjAl3gAABAgQIEFgJGBMrPocJECBAgAABY8IdIECAAAECBFYCxsS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/PHq5/V4Vk7EdCHEyDQLeArE939SX9 geOvgL43M1898aWPQfHcHT/bw7N2IqgPJ0CgR8AbXE9Xkn5 Aseg MXMfPcT/5PHz C4b2Acv81x0w8KO3666PHv/CJAgMBTKWBMPJW1 qTOJHD9fBxj4KWZ d09r3t8/O9n5vgumZ/8dZz74ZkyeRkCBAjECRgTcZUIFCzwp5n5zi35jt/m ObMHF/VePNqfBwfegyJ12fm/eDPSzQCBAisBIyJFZ/DXzCBTw F60//HzPz05n5wxfMw6dLgACB/wkYEy4CgdMFj ..."}

img_str = re.search(r'base64,(.*)', DATA['png-data']).group(1)
img_data = base64.b64decode(img_str)

with open(OUTPUT_FILE, 'wb') as f:
    f.write(img_data)