Source code for odoo_xmlrpc_twisted.support.support_remove_background
"""Supporting program to test the module :func:`odoo_xmlrpc_twisted.functions.remove_background`."""importsysimportosimportioimportbase64fromPILimportImage# Add functions directory to path for importing custom modulessys.path.append(os.path.join(os.path.dirname(__file__),'..','functions'))fromremove_backgroundimportremove_background# Set the name and path of the resulting image filecurrent_path=os.path.dirname(os.path.realpath(__file__))filename='picture_remove_backgound.png'fullpath=os.path.join(current_path,filename)
[docs]defsupport_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 streamstream=io.BytesIO()# Construct path to test directory where the image is locatedrepo_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 imageimage=Image.open(image_path)image.save(stream,format="PNG")# convert PIL image to base64 stringimg_str=base64.b64encode(stream.getvalue())custom_datas=img_str.decode('ascii')# call the function "remove_background" to remove the background from the pictureremoved_custom_datas=remove_background(custom_datas)# converting the picture from a base64 stringimage_str=base64.b64decode(removed_custom_datas)# save it as a picture in the actual path withopen(fullpath,'wb')asimageFile:imageFile.write(image_str)return(filename)