[英]Save an image from a Generator object - Python
我調用了 API 將圖像轉換為自身的縮略圖版本,並返回了生成器 object。 但我不知道如何將 object 保存為本地計算機上的圖像。
我從 API 的文檔中得到“成功的響應包含縮略圖二進制文件”,但我不知道如何訪問它。 我在想,所以我需要將二進制文件轉換為字符串或列表,然后使用 PIL 的 Image class 將其轉換為圖像?
我不知道最好的方法。 我知道生成器只是保存 state 的迭代器,但這對於其中的圖像數據和訪問數據而言意義不大,因此我在本地文件夾中有一個保存的圖像。
這是我的代碼:
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
# Get a local image
local_image_path_thumb = "resources\\objects.jpg"
local_image_thumb = open(local_image_path_objects, "rb")
print("Generating thumbnail from a local image...")
# Call the API with a local image, set the width/height if desired (pixels)
# Returns a Generator object, a thumbnail image binary.
thumb_local = computervision_client.generate_thumbnail_in_stream(100, 100, local_image_thumb, True)
# Save the thumbnail to your local root folder of this project.
# Save to here, somehow: "\\resources\\thumb_local.jpg"
print("Thumbnail saved to local folder.")
這是 function generate_thumbnail_in_stream的 API 文檔。
with open("output_file.png", "wb") as fp:
for chunk in thumb_local:
fp.write(chunk)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.