Skip to main content

Calculate image tokens for (Azure) OpenAI models. Offers multiple utilities to resize images to reduce token usage.

Project description

pictoken

Calculate image tokens for Azure OpenAI models. This utility helps resize images to minimize token usage. Unlike openai/tiktoken, it isn't a tokenizer but calculates image tokens for specific requests. It supports OpenAI models such as gpt-4o-mini and gpt-4o.

Based on calculation costs for vision capabilities for OpenAI models. In the future models from other providers might be supported as well.

Installation

Requires Python 3.9 or later.

pip install pictoken

Usage

Calculate image tokens

You can provide images in two main ways: by passing a link to the image or by passing the base64 encoded image directly in the request.

Please note that passing the model is required, as the image token calculation is model-specific. The dollar price per image is the same for GPT-4o and GPT-4o mini. To maintain this, GPT-4o mini uses more tokens per image.. These additional tokens are only used for billing purposes and do not affect the context limit of the model.

By image

import pictoken

image_tokens_from_url = pictoken.calculate_image_tokens_by_image(image="http://", model="gpt-4o-mini")
image_tokens_from_base64 = pictoken.calculate_image_tokens_by_image(image="data:image/png;base64,...", model="gpt-4o-mini")

By image dimensions

import pictoken

image_tokens = pictoken.calculate_image_tokens_by_image_dimensions(
    width=2048, height=2048, model=pictoken.Model.GPT_4O, detail=pictoken.ImageDetail.HIGH
)

print(image_tokens)
# Response(total_tokens=765, base_tokens=85, tile_tokens=680, total_tiles=4, width_tiles=2, height_tiles=2, resized=ResizedImage(width=768, height=768))

Resize images

For low res mode, OpenAI expects a 512px x 512px image. For high res mode, the short side of the image should be less than 768px and the long side should be less than 2,000px. The latency of the model can also be improved by downsizing your images ahead of time to be less than the maximum size they are expected them to be.

#TODO

Add visual metadata to image

The model doesn't process original file names or metadata, and images are resized before analysis, affecting their original dimensions. This utility adds a small text overlay to the image. This can be useful for models to reference to the specific image in a request with multiple images.

#TODO

Development

Contributions are welcome. This packages uses uv for dependency management and packaging. If you use Visual Studio Code with Docker or GitHub CodeSpaces, you can leverage the included devcontainer. This will install all required dependencies and tools and has the right Python version available. Easy!

To manually set up the development environment, make sure you have uv installed and run uv sync to install all dependencies.

License

MIT

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

pictoken-0.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

pictoken-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file pictoken-0.1.0.tar.gz.

File metadata

  • Download URL: pictoken-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.18

File hashes

Hashes for pictoken-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f38a2eb236aeebb731a7304a6d0b05c2058b2bd84595a9a2accb2c12f923c4d
MD5 df40afb56e4202123ff03dcc00627207
BLAKE2b-256 44b3315bf8145c4504e7fc343aaa8dab15c077f4f8b40864c445654d0bf954e4

See more details on using hashes here.

Provenance

File details

Details for the file pictoken-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pictoken-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91c32a5630f13694383e45d3b83324bc0f726cd07c4de665b0389c56be2bb663
MD5 675dd1663e272ab72fdca2d47e4ca524
BLAKE2b-256 2f7ca052d7db35f8d128bd43b662321276fddd72332d578b7efd389e2e3e891f

See more details on using hashes here.

Provenance

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