我进行了API调用,将图像转换为自身的缩略图版本,并返回了Generator对象。但是我不知道如何将该对象另存为本地计算机上的图像。
我从API文档中得到“成功的响应包含缩略图图像二进制文件”,但是我不知道如何访问它。我在想,所以我需要将二进制文件转换为字符串或列表,然后使用PIL中的Image类将其转换为图像?
我不知道最好的方法。我知道生成器只是保存状态的迭代器,但是对于其中包含图像数据并访问数据以使我在本地文件夹中保存图像的意义不大。
这是我的代码:
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.")
这是功能generate_thumbnail_in_stream的API文档。