def create_thumb_with_azure_vision(self, infile: str,
                                       outfile: str) -> None:
        """Send infile to Azure Computer Vision API, return new thumb."""
        logging.debug("Enter Google vision API")

        # Get credentials for Azure vision API
        subscription_key = os.getenv("AZURE_VISION_SUBSCRIPTION_KEY")
        endpoint = os.getenv("AZURE_VISION_ENDPOINT")

        # Connect to service API
        computervision_client = ComputerVisionClient(
            endpoint, CognitiveServicesCredentials(subscription_key))

        # Open local image file
        local_image = io.open(infile, "rb")
        thumb_width = int(get_global_setting("PHOTO_THUMB_WIDTH"))
        thumb_heigth = int(get_global_setting("PHOTO_THUMB_HEIGTH"))

        # Returns a Generator object, a thumbnail image binary (list).
        thumb_local = computervision_client.generate_thumbnail_in_stream(
            thumb_width, thumb_heigth, local_image, True)

        # Write the image binary to file
        with open(outfile, "wb") as f:
            for chunk in thumb_local:
                f.write(chunk)
computervision_client = ComputerVisionClient(
    endpoint, CognitiveServicesCredentials(subscription_key))
'''
Generate Thumbnail
This example creates a thumbnail from both a local and URL image.
'''
print("===== Generate Thumbnails - local =====")

# Generate a thumbnail from a local image
local_image_path_thumb = "Images/Faces.jpg"
local_image_thumb = open(local_image_path_thumb, "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 (list).
thumb_local = computervision_client.generate_thumbnail_in_stream(
    100, 100, local_image_thumb, True)

# Write the image binary to file
with open("thumb_local.png", "wb") as f:
    for chunk in thumb_local:
        f.write(chunk)

# Uncomment/use this if you are writing many images as thumbnails from a list
# for i, image in enumerate(thumb_local, start=0):
#      with open('thumb_{0}.jpg'.format(i), 'wb') as f:
#         f.write(image)

print("Thumbnail saved to local folder.")
print()
print("===== Generate Thumbnails - remote =====")
# Generate a thumbnail from a URL image
Esempio n. 3
0
        if urllib.request.urlopen(req).status == 200:
            try:
                analysis = client.generate_thumbnail(width, height, url)
            except Exception as e:
                catch_exception(e, url)

        sname = re.sub('\.(\w+)$', r'-thumbnail.\1',
                       os.path.basename(urlparse(url).path))
        sname = os.path.join(get_cmd_cwd(), sname)

    except urllib.error.URLError:
        sys.exit("Error: The URL does not appear to exist. Please check."
                 f"{url}")
else:
    path = os.path.join(get_cmd_cwd(), url)
    with open(path, 'rb') as fstream:
        try:
            analysis = client.generate_thumbnail_in_stream(
                width, height, fstream)
        except Exception as e:
            catch_exception(e, path)

    sname = re.sub('\.(\w+)$', r'-thumbnail.\1', path)

for x in analysis:
    image = Image.open(io.BytesIO(x))

image.save(sname)
print(sname)