Source code for odoo_xmlrpc_twisted.functions.crop_hints
"""Crop an image."""fromgoogle.cloudimportvisionfromgoogle.oauth2importservice_accountfromPILimportImagefromioimportBytesIOimportosimportbase64importconfigparser# ------- for the initialisation get parameters from the File "parameter_google.ini" -------# Name and full path of the Parameter Filefile_name="parameter_google.ini"path_name=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))# Go up to repo rootconfig_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 Filegoogle_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 Fileaspect_ratio=config.get('handle_picture','aspect_ratio')aspect_ratio_value=float(aspect_ratio)# Instantiates a client for Google Cloud Vision APIvision_client=vision.ImageAnnotatorClient(credentials=Google_Credentials)# ------- end of the initialisation -------
[docs]defcrop_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_imagecrop_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 imagetry:vertices=hints[0].bounding_poly.verticesexcept(IndexError,KeyError):# No crop hints available, return original imagereturnimg_str# Get bounds for the first crop hint using the defined aspect ratiovects=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 PILimage_before=Image.open(BytesIO(base64.b64decode(img_str)))# Crop the hints of the imageimage_cropped=image_before.crop([vects[0].x,vects[0].y,vects[2].x-1,vects[2].y-1])# convert PIL image to base64 stringbuffered=BytesIO()image_cropped.save(buffered,format="JPEG")image_result=base64.b64encode(buffered.getvalue())return(image_result)