Skip to main content

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,
    RejectionNoticeToHumanEnum,
)
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
    rejection_notice_enum = rejection_notice.rejection_notice
    human_readable = RejectionNoticeToHumanEnum[
        rejection_notice_enum.name
    ].value
    print(
        f'Failure: {human_readable} '
        f'rejection_notice: { rejection_notice.rejection_notice}, '
        f'inner_error: {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,
    RejectionNoticeToHumanEnum,
)
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
    rejection_notice_enum = rejection_notice.rejection_notice
    human_readable = RejectionNoticeToHumanEnum[
        rejection_notice_enum.name
    ].value
    print(
        f'Failure: {human_readable} '
        f'rejection_notice: { rejection_notice.rejection_notice}, '
        f'inner_error: {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:

Both the light and the lux API are supported as you could see above.

Project details


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.2.7.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

toilmore_sdk-0.2.7-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file toilmore-sdk-0.2.7.tar.gz.

File metadata

  • Download URL: toilmore-sdk-0.2.7.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.26.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.5

File hashes

Hashes for toilmore-sdk-0.2.7.tar.gz
Algorithm Hash digest
SHA256 180c730a3545da7853746872717211af49fb8698cf2551961436e21a4f8aac0a
MD5 a367818f97e4b86c000b8ec9d1cb22c8
BLAKE2b-256 65043c9a823c1d294755303119425ec16f75ede3894e3057a9db7c5d8aaf1c2b

See more details on using hashes here.

File details

Details for the file toilmore_sdk-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: toilmore_sdk-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.26.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.5

File hashes

Hashes for toilmore_sdk-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d4734913dfaa6f3a54eeb9ec2870a7a2bafc16fb0cedda361b3ff48bf66a88a9
MD5 47a8c9c09c7acda08ee2a1e1a16b20c0
BLAKE2b-256 d9d050136fee3b9d1ff95359fb52943bb03bce3f65e8e2a9c39288b38a2bd051

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page