Source code for odoo_xmlrpc_twisted.support.support_remove_background

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

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 remove_background import remove_background

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


[docs] def support_remove_background(custom_image_name): """ Function to test the module "remove_background" A picture stored in the current directory will be captured into a stream. The stream will be handled over to the module "remove_background". The result from that module will be save as a "PNG" image file to check if "remove_background" 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="PNG") # convert PIL image to base64 string img_str = base64.b64encode(stream.getvalue()) custom_datas = img_str.decode('ascii') # call the function "remove_background" to remove the background from the picture removed_custom_datas = remove_background(custom_datas) # converting the picture from a base64 string image_str = base64.b64decode(removed_custom_datas) # save it as a picture in the actual path with open(fullpath, 'wb') as imageFile: imageFile.write(image_str) return(filename)