Source code for odoo_xmlrpc_twisted.support.support_crop_hints

"""
Supporting program to test the module :func:`odoo_xmlrpc_twisted.functions.crop_hints`.
"""

import sys
import os
import io
import base64
from PIL import Image

# Add functions directory to path for importing custom modules
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'functions'))
from crop_hints import crop_hints

# Set the name and path of the resulting image file
current_path = os.path.dirname(os.path.realpath(__file__))
filename = 'picture_crop_hints.png'
fullpath = os.path.join(current_path, filename)


[docs] def support_crop_hints(custom_image_name): """ Function to test the module "crop_hints" A picture stored in the current directory will be captured into a stream. The stream will be handled over to the module "crop_hints". The result from that module will be save as a "PNG" image file to check if "crop_hints" works as expected. """ # for the capturing of the image to a BytesIO stream stream = io.BytesIO() # Construct path to test directory where the image is located repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) test_dir = os.path.join(repo_root, 'test') image_path = os.path.join(test_dir, custom_image_name) # Open an image and save it as a PIL image image = Image.open(image_path) image.save(stream, format="JPEG") # convert PIL image to base64 string img_str = base64.b64encode(stream.getvalue()) custom_datas = img_str.decode('ascii') # call the function "crop_hints" to crop_hints the picture crop_custom_datas = crop_hints(custom_datas) # converting the picture from a base64 string image_str = base64.b64decode(crop_custom_datas) # save it as a picture in the actual path with open(fullpath, 'wb') as imageFile: imageFile.write(image_str) return(filename)