Source code for odoo_xmlrpc_twisted.functions.crop_hints

"""
Crop an image.
"""

from google.cloud  import vision
from google.oauth2 import service_account
from PIL           import Image
from io            import BytesIO
import os
import base64
import configparser


# ------- for the initialisation get parameters from the File "parameter_google.ini" -------

# Name and full path of the Parameter File
file_name  = "parameter_google.ini"
path_name  = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # Go up to repo root
config_path = os.path.join(path_name, 'config')
fullpath   = os.path.join(config_path, file_name)

config     = configparser.ConfigParser()
config.read(fullpath)


# get the Google Credentials from the Parameter File
google_file_name        = config.get('credentials_google', 'credentials_filename')
Google_Credentials_File = os.path.join(config_path, google_file_name)
Google_Credentials      = service_account.Credentials.from_service_account_file(Google_Credentials_File)

# get the aspect ratio for the "Crop Hints" function from the Parameter File
aspect_ratio            = config.get('handle_picture', 'aspect_ratio')
aspect_ratio_value      = float(aspect_ratio)

# Instantiates a client for Google Cloud Vision API
vision_client           = vision.ImageAnnotatorClient(credentials=Google_Credentials)


# ------- end of the initialisation ------- 


[docs] def crop_hints(image): """ Crops the image using the "Crop Hints" function from Google Cloud Vision API, see https://cloud.google.com/vision/docs/crop-hints#authenticating_to_the_api. Result is a cropped image stream. """ img_str = image # convert base64 string (= img_str) to PIL image (= image) image = vision.Image(content=base64.b64decode(img_str)) # Getting crop hint annotations for the image # see https://cloud.google.com/vision/docs/crop-hints#getting_crop_hint_annotations_for_the_image crop_hints_params = vision.CropHintsParams(aspect_ratios=[aspect_ratio_value]) image_context = vision.ImageContext(crop_hints_params=crop_hints_params) response = vision_client.crop_hints(image=image, image_context=image_context) hints = response.crop_hints_annotation.crop_hints # If no crop hints are returned, return the original image try: vertices = hints[0].bounding_poly.vertices except (IndexError, KeyError): # No crop hints available, return original image return img_str # Get bounds for the first crop hint using the defined aspect ratio vects = vertices # The crop_to_hint method crops the image using the suggested crop hint # see https://cloud.google.com/vision/docs/crop-hints#using_the_response_to_crop_or_draw_the_hints_bounding_box # Convert the image to PIL image_before = Image.open(BytesIO(base64.b64decode(img_str))) # Crop the hints of the image image_cropped = image_before.crop([vects[0].x, vects[0].y, vects[2].x - 1, vects[2].y - 1]) # convert PIL image to base64 string buffered = BytesIO() image_cropped.save(buffered, format="JPEG") image_result = base64.b64encode(buffered.getvalue()) return(image_result)