读取s3图片并保存至excel

1. 构建Client类,实现图片读取

1.1 导包&config

client.py

import os
import base64
import numpy as np
import pandas as pd
import cv2
import boto3
# boto3安装: pip3 install opencv-python boto3

config = {
    "region_name": "us-east-1",
    "endpoint_url": "https://s3.rapidcompute.com",
    # "image_bucket": "prod-barwaqt-image",
    "aws_access_key_id": "rcus_bd-prod",
    "aws_secret_access_key": "OgRKm6h...2HdbKA6s",
}

1.2 类实现

class Client:
    def __init__(self):
        self.config = config
        self.client = boto3.client('s3', **self.config)

    def read_image(self, bucket_name, image_key):
        try:
            response = self.client.get_object(Bucket=bucket_name, Key=image_key)
            body = response.get('Body')
            tmp = np.frombuffer(body.read(), np.uint8)
            image = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
            return 'OK', image
        except Exception as e:
            return 'ERROR', 'READ_IMAGE_ERROR'

    def read_image_b64(self, bucket_name, image_key):
        status, image = self.read_image(bucket_name, image_key)
        if status == 'OK':
            retval, buffer = cv2.imencode('.jpg', image)
            pic_str = base64.b64encode(buffer)
            return status, pic_str.decode()
        else:
            return status, image</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值