Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

amazon web services - How to read image using OpenCV got from S3 using Python 3?

I have a bunch of images in my S3 bucket folder.

I have a list of keys from S3 (img_list) and I can read and display the images:

key = img_list[0]
bucket = s3_resource.Bucket(bucket_name)
img = bucket.Object(key).get().get('Body').read()

I have a function for that:

def image_from_s3(bucket, key):

    bucket = s3_resource.Bucket(bucket)
    image = bucket.Object(key)
    img_data = image.get().get('Body').read()

    return Image.open(io.BytesIO(img_data))

Now what I want is to read the image using OpenCV but I am getting an error:

key = img_list[0]
bucket = s3_resource.Bucket(bucket_name)
img = bucket.Object(key).get().get('Body').read()
cv2.imread(img)


SystemError                               Traceback (most recent call last)
<ipython-input-13-9561b5237a85> in <module>
      2 bucket = s3_resource.Bucket(bucket_name)
      3 img = bucket.Object(key).get().get('Body').read()
----> 4 cv2.imread(img)

SystemError: <built-in function imread> returned NULL without setting an error

Please advise how to read it in the right way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sorry, I got it wrong in the comments. This code sets up a PNG file in a memory buffer to simulate what you get from S3:

#!/usr/bin/env python3

from PIL import Image, ImageDraw
import cv2

# Create new solid red image and save to disk as PNG
im = Image.new('RGB', (640, 480), (255, 0, 0))
im.save('image.png')

# Slurp entire contents, raw and uninterpreted, from disk to memory
with open('image.png', 'rb') as f:
   raw = f.read()

# "raw" should now be very similar to what you get from your S3 bucket

Now, all I need is this:

nparray = cv2.imdecode(np.asarray(bytearray(raw)), cv2.IMREAD_COLOR)

So, you should need:

bucket = s3_resource.Bucket(bucket_name)
img = bucket.Object(key).get().get('Body').read()
nparray = cv2.imdecode(np.asarray(bytearray(img)), cv2.IMREAD_COLOR)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...