将numpy数组作为灰度图像上传到S3存储桶

问题描述 投票:0回答:2

我使用numpy对python中的一些灰度图像进行了一些数学运算。

现在我想将生成的numpy数组作为png图像上传到我的S3存储桶。我试图将它们作为base64格式上传,但是这样我就无法将它们作为S3中的图像打开。我的代码如下:

dec=base64.b64decode(numpy_image)
s3.Bucket('bucketname').put_object(Key='image.png',Body=dec, ContentType='image/png',ACL='public-read')

当我尝试从S3打开文件时,它表示该文件包含错误

python numpy amazon-s3 bucket
2个回答
1
投票

所以我需要先将numpy数组转换为图像。以下代码可行:

from PIL import Image
import io
img = Image.fromarray(numpy_image).convert('RGB')
out_img = BytesIO()
img.save(out_img, format='png')
out_img.seek(0)  
s3.Bucket('my-pocket').put_object(Key='cluster.png',Body=out_img,ContentType='image/png',ACL='public-read')

0
投票

这适用于使用CV2库

data_serial = cv2.imencode('.png', frame)[1].tostring()
s3.Object(bucket_name, key_path).put(Body=data_serial,ContentType='image/PNG')
© www.soinside.com 2019 - 2024. All rights reserved.