Skip to main content

imagetoken

Utility to estimate the number of tokens in an image or directory of images for OpenAI models.

Install using pip

pip install image-token

Supported models

  • gpt-4.1-mini
  • gpt-4.1-nano
  • gpt-4.1
  • o4-mini
  • gpt-4o
  • gpt-4o-mini
  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite
  • gemini-2.5-flash-image-preivew
  • gemini-2.0-flash
  • gemini-2.0-flash-lite
  • gemini-1.5-pro
  • gemini-1.5-flash

Usage

To get the number of tokens for a single image

from image_token import get_token
num_tokens = get_token(model_name="gpt-4.1-mini", path=r"kitten.jpg")

To get the number of tokens for a directory of images

from image_token import get_token
num_tokens = get_token(model_name="gpt-4.1-mini", path=r"image_folder")

To get the number of tokens for a URL

from image_token import get_token
num_tokens = get_token(model_name="gpt-4.1-mini", path=r"https://raw.githubusercontent.com/srinathmkce/imagetoken/main/Images/kitten.jpeg")

To get the number of token for multiple URLS

for image_token import get_token
urls = ["https://raw.githubusercontent.com/srinathmkce/imagetoken/main/Images/kitten.jpeg"
,"https://raw.githubusercontent.com/srinathmkce/imagetoken/main/Images/kitten.jpg"
,"https://raw.githubusercontent.com/srinathmkce/imagetoken/main/Images/kitten.png"
]
num_tokens = get_token(model_name="gpt-4.1-mini",path=urls)

To get the estimated cost of generating text from an image or directory of images

from image_token import get_cost
cost = get_cost(model_name="gpt-4.1-nano", system_prompt_tokens=300 * 100, approx_output_tokens=100 * 100, path=r"image_folder")

Langchain integration

You can simulate the langchain OpenAI call and calculate the input token and cost

import base64
from pathlib import Path
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from image_token import simulate_image_token_cost


llm = ChatOpenAI(model="gpt-4.1-nano")

path = str(Path("tests") / "image_folder" / "kitten.jpg")

with open(path, "rb") as image_file:
    image_bytes = image_file.read()
    image_base64 = base64.b64encode(image_bytes).decode("utf-8")

image_data_url = f"data:image/jpeg;base64,{image_base64}"

messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(
        content=[
            {"type": "image_url", "image_url": {"url": image_data_url}},
        ],
    ),
]

result = simulate_image_token_cost(llm, messages)

print(result)

You can simulate the langchain OpenAI call and calculate the input token and cost using URL

import base64
from pathlib import Path
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from image_token import simulate_image_token_cost

llm = ChatOpenAI(model="gpt-4.1-nano")

image_data_url = "https://raw.githubusercontent.com/srinathmkce/imagetoken/main/Images/kitten.jpeg"

messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(
        content=[
            {"type": "image_url", "image_url": {"url": image_data_url}},
        ],
    ),
]

result = simulate_image_token_cost(llm, messages)

print(result)

Note: simulate_image_token_cost mocks the LangChain OpenAI API and returns the result without making an actual request to the LangChain endpoint. You can simulate the call before executing llm.invoke(messages). The token count and cost calculated are based only on the input tokens. To get the accurate cost, make sure to include the output tokens as well.

Run unit tests

Perform test using poetry package manager

Add the package using the command

poetry add <package_name>

Install required packages using

poetry install

Create an .env file in the project root directory and set the openai key OPENAI_API_KEY

Run pytests with the following command

poetry run pytest -sv tests

Metadata

Release files for image-token 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for image-token 0.1.6
File Size Uploaded Provenance Actions
image_token-0.1.6.tar.gz 25.9 kB Without provenance Details for image_token-0.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for image-token 0.1.6
File Interpreter ABI Platform Actions
image_token-0.1.6-py3-none-any.whl Python 3 none any Details for image_token-0.1.6-py3-none-any.whl

Total release size: 48.3 kB

Release files / image_token-0.1.6.tar.gz

Download URL image_token-0.1.6.tar.gz
Size 25.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ce9072c22f0f691da48a308d6b2c1a54cc572b7574974cab1dfe51d8ea96cd02
BLAKE2b-256 checksum
How to use checksums
982504ade5b251210a3ecddfc8a13db60e51a7d18d575b686bdc07083ca5f586
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release files / image_token-0.1.6-py3-none-any.whl

Download URL image_token-0.1.6-py3-none-any.whl
Size 22.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
223796aab542156208c11f5771031b78ca42031d853a691f56e7cdbe1da6f2be
BLAKE2b-256 checksum
How to use checksums
d797e819bfc5df692cedcec0712c0a50ab0052cd688b4c6473e92ab1f247dcc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Security for image-token 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page