You need to enable JavaScript to run this app.
对象存储

对象存储

复制全文
图片处理
获取图片信息(Python SDK)
复制全文
获取图片信息(Python SDK)

获取图片信息功能可以获取到图片的基本信息,包括图片的长、宽、大小和格式。如果图片包含 Exif 信息,将按照 JSON 格式返回内容。本文介绍如何通过 TOS Python SDK 获取图片信息。

示例代码

以下代码展示如何获取图片信息。

import json
import os
import tos
from tos.enum import TierType
from tos.models2 import RestoreJobParameters

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
# 填写 Bucket 所在区域对应的 Endpoint。如果以华北2(北京)为例,则 your endpoint 填写为 tos-cn-beijing.volces.com,your region 填写为 cn-beijing。
endpoint = "your endpoint"
region = "your region"
bucket_name = "bucket-test"
image_key = "image.png"
style = "image/info"

try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现。
    client = tos.TosClientV2(ak, sk, endpoint, region)
    client.put_object_from_file(bucket=bucket_name, key=image_key, file_path=image_key)
    object_stream = client.get_object(bucket=bucket_name, key=image_key, process=style)
    image_info = json.load(object_stream)
    print(image_info)
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常。
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息。
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题,强烈建议日志中保存。
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))

相关文档

关于获取图片信息的详细介绍,请参见获取信息

最近更新时间:2024.02.04 18:31:08
这个页面对您有帮助吗?
有用
有用
无用
无用