Package: toilmore-sdk
Toilmore Python APIInstallingpip install toilmore-sdkHow to useSimplifies using the toilmore API by wrapping the request state machine.
To optimize an image, all you need is a valid API token, and domain.
Seeherehow to get them.Using the light APIimportosimportasynciofromtoilmoresdkimport(LIGHT_API,Toilmore,)fromtoilmoresdk.submit_machineimport(OptimizationResponseStatus,RejectionNoticeToHumanEnum,)fromtoilmoresdk.stream_helpersimportstore_file_contentfromtoilmoresdk.api_configimportApiConfigfromtoilmoresdk.constantsimportPrecursorEnumfromtoilmoresdkimportconstantsconfig=ApiConfig(# LIGHT_API contains our light api endpoint.api_endpoint=LIGHT_API,# Use a valid API token below:api_token='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',# Use a valid domain below, as received when# you created the token.domain='YYYYYYYYYYYYYYYYYYYYYYYYYY')toilmore=Toilmore(config)loop=asyncio.get_event_loop()image_path="./my_image.jpg"precursor=PrecursorEnum.WEBP0r=loop.run_until_complete(toilmore.optimize(image_path,precursor))ifr.status==OptimizationResponseStatus.FAILURE:rejection_notice=r.rejection_noticerejection_notice_enum=rejection_notice.rejection_noticehuman_readable=RejectionNoticeToHumanEnum[rejection_notice_enum.name].valueprint(f'Failure:{human_readable}'f'rejection_notice:{rejection_notice.rejection_notice}, 'f'inner_error:{rejection_notice.inner_error}')elifr.status==OptimizationResponseStatus.SUCCESS:base_filename,file_extension=os.path.splitext(image_path)output_filename='{}{}'.format(base_filename,constants.PrecursorToExtensionEnum[precursor.name].value)output_dir=os.path.join(os.path.abspath(os.path.dirname(image_path)),output_filename)loop.run_until_complete(store_file_content(r.response_stream,output_dir))print('Optimized image stored at: ',output_dir)Using the Lux APIimportosimportasynciofromtoilmoresdkimport(LUX_API,Toilmore,)fromtoilmoresdk.submit_machineimport(OptimizationResponseStatus,RejectionNoticeToHumanEnum,)fromtoilmoresdk.stream_helpersimportstore_file_contentfromtoilmoresdk.api_configimportApiConfigfromtoilmoresdk.constantsimportPrecursorEnumfromtoilmoresdkimportconstantsconfig=ApiConfig(# LIGHT_API contains our light api endpoint.api_endpoint=LUX_API,# Use a valid API token below:api_token='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',# Use a valid domain below, as received when# you created the token.domain='YYYYYYYYYYYYYYYYYYYYYYYYYY')toilmore=Toilmore(config)loop=asyncio.get_event_loop()image_path="./my_image.jpg"precursor=PrecursorEnum.WEBP0adjustments={"shifter":{"steps":[{"scale-to":{"width":90}}]},"encoder":{"quality-measure":"fsim-c","qual-threshold":0.90}}r=loop.run_until_complete(toilmore.optimize(image_path,precursor,adjustments))ifr.status==OptimizationResponseStatus.FAILURE:rejection_notice=r.rejection_noticerejection_notice_enum=rejection_notice.rejection_noticehuman_readable=RejectionNoticeToHumanEnum[rejection_notice_enum.name].valueprint(f'Failure:{human_readable}'f'rejection_notice:{rejection_notice.rejection_notice}, 'f'inner_error:{rejection_notice.inner_error}')elifr.status==OptimizationResponseStatus.SUCCESS:base_filename,file_extension=os.path.splitext(image_path)output_filename='{}{}'.format(base_filename,constants.PrecursorToExtensionEnum[precursor.name].value)output_dir=os.path.join(os.path.abspath(os.path.dirname(image_path)),output_filename)loop.run_until_complete(store_file_content(r.response_stream,output_dir))print('Optimized image stored at: ',output_dir)To know more about thelux's adjustments, please take a look to the following doc:https://demo.pixellena.com/adjustments/https://pixellena.com/docs/quality-measures/https://pixellena.com/docs/enhance/https://pixellena.com/docs/master/https://pixellena.com/docs/encoder/Both the light and the lux API are supported as you could see above.