我用Python生成了一个图像,我需要将这个Pil image转换成Base64,而不是将这个保存到任何文件夹中……
我有一些数据,我用下面的代码行得到RGB img:
img = Image.fromarray(data,'RGB')什么是简单的方法来转换这个文件到base64?(我不能打开一个文件图像,因为我不能保存的img)?
谢谢你的帮助
使用Node,我可以使用以下代码行获得正确的base64:
pythonShell= require("python-shell");
app.post('/index/gen/',urlencodedParser, function (req,res){
pythonShell.run('minigen.py', function (err, results) {
if (err) throw err;
var img = base64img.base64Sync('./images/miniature.jpg');
res.send(img); });
}) 但是如果我使用NodeJS,我必须保存这个文件...
这是从图像生成矩阵的代码,您不需要知道数据中有什么;)
image = Image.open("./carte/"+fichier)
image = image.resize((400,400),Image.ANTIALIAS)
w,h = image.size
tab = numpy.array(image)
data = numpy.zeros((h, w, 3), dtype=numpy.uint8)发布于 2020-04-09 12:48:20
@florian answer对我帮助很大,但是base64.b64encode(img_byte)返回字节,所以我需要在连接之前将其解码为字符串(使用Python3.6):
def img_to_base64_str(self, img):
buffered = BytesIO()
img.save(buffered, format="PNG")
buffered.seek(0)
img_byte = buffered.getvalue()
img_str = "data:image/png;base64," + base64.b64encode(img_byte).decode()https://stackoverflow.com/questions/48229318
复制相似问题