简体   繁体   English

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

[英]Upload numpy array as grayscale image to S3 bucket

I have made some mathematical operations on some grayscaled images in python using numpy. 我已经使用numpy对python中的某些灰度图像进行了一些数学运算。

Now I want to upload the resulting numpy arrays as png images to my S3 bucket. 现在,我想将生成的numpy数组作为png图像上传到我的S3存储桶。 I have tried to upload them as base64 formats, but in that way I cannot open them as images from S3. 我试图将它们上传为base64格式,但是那样我无法将它们作为S3中的图像打开。 My code looks as follows: 我的代码如下所示:

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

When I try to open the file from S3 it says that the file contains an error 当我尝试从S3打开文件时,它说文件包含错误

So I needed to convert the numpy array into an image first. 因此,我需要先将numpy数组转换为图像。 The following code turned out to work: 以下代码可以正常工作:

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')

This works using the CV2 library 这可以在CV2库中使用

data_serial = cv2.imencode('.png', frame)[1].tostring()
s3.Object(bucket_name, key_path).put(Body=data_serial,ContentType='image/PNG')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM