The Toilmore API SDK provides Python APIs to optimize and process images.
Project description
Toilmore Python API
Installing
pip install toilmore-sdk
How to use
Simplifies using the toilmore API by wrapping the request state machine. To optimize an image, all you need is a valid API token, and domain. See here how to get them.
Using the light API
import os
import asyncio
from toilmoresdk import (
LIGHT_API,
Toilmore,
)
from toilmoresdk.submit_machine import OptimizationResponseStatus
from toilmoresdk.stream_helpers import store_file_content
from toilmoresdk.api_config import ApiConfig
from toilmoresdk.constants import PrecursorEnum
from toilmoresdk import constants
config = 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.WEBP0
r = loop.run_until_complete(
toilmore.optimize(image_path, precursor)
)
if r.status == OptimizationResponseStatus.FAILURE:
rejection_notice = r.rejection_notice
print(
'rejection_notice: {}, inner_error: {}'.format(
rejection_notice.rejection_notice,
rejection_notice.inner_error
)
)
elif r.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 API
import os
import asyncio
from toilmoresdk import (
LUX_API,
Toilmore,
)
from toilmoresdk.submit_machine import OptimizationResponseStatus
from toilmoresdk.stream_helpers import store_file_content
from toilmoresdk.api_config import ApiConfig
from toilmoresdk.constants import PrecursorEnum
from toilmoresdk import constants
config = 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.WEBP0
adjustments = {
"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)
)
if r.status == OptimizationResponseStatus.FAILURE:
rejection_notice = r.rejection_notice
print(
'rejection_notice: {}, inner_error: {}'.format(
rejection_notice.rejection_notice,
rejection_notice.inner_error
)
)
elif r.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 the lux
'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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
toilmore-sdk-0.0.7.tar.gz
(18.1 kB
view hashes)
Built Distribution
Close
Hashes for toilmore_sdk-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7dff867848dab17b3d1a07373abbaec8d7f6126b7a27dc959a8f1791dc81fe7 |
|
MD5 | 880be57153b692200a0618c57c0fbf19 |
|
BLAKE2b-256 | a63e7f82752ee8df7e6323ba022a22f2159a52db30388f77c4e6cd6afb554c5c |