擅长:python、mysql、java
<p>使用OpenCV调整图像大小并将其编码为字节,然后让Boto3将这些字节上传到S3,并为缩略图指定一个新的键名:</p>
<pre><code>import boto3
import cv2
BUCKET_NAME = "MY_BUCKET_NAME"
# Load original image from disk
original_image_filename = "original_image.png"
original_image = cv2.imread(original_image_filename)
# Resize the original image to obtain a thumbnail image,
# and encode this thumbnail to bytes in the PNG format
thumbnail_image = cv2.resize(original_image, (100, 100))
thumbnail_image_png = cv2.imencode(".png", thumbnail_image)[1].tostring()
# Upload the PNG bytes of the thumbnail to S3
s3 = boto3.resource("s3")
thumbnail_image_filename = "thumbnail.png"
object = s3.Object(BUCKET_NAME, thumbnail_image_filename)
object.put(Body=thumbnail_image_png, ContentType="image/png")
</code></pre>