Skip to main content

MIT License CI PyPI PyPI - Python Version PyPI - Wheel

Tinify API client for Python

Tinify is the official Python client for the TinyPNG and TinyJPG image compression API, enabling developers to intelligently compress, resize, convert and optimize PNG, APNG, JPEG, WebP, AVIF and JPEG XL images programmatically. Read more at https://tinify.com.

Go to the full documentation for the Python client.

Features

  • Compress and optimize images, reducing file size by 50-80% while preserving visual quality
  • Resize and crop images with smart compression
  • Convert between PNG, JPEG, WebP, AVIF and JPEG XL formats
  • Preserve metadata (optional)
  • Upload to storage providers like Amazon S3, Google cloud storage.
  • Apply visual transformations with the Tinify API
  • Comprehensive error handling

Requirements

  • Python 2.7+
  • Requests library

Installation

Install the API client with pip:

pip install tinify

Quick start

import tinify

# Set your API key (get one for free at https://tinypng.com/developers)
tinify.key = "YOUR_API_KEY"

# Compress an image from a file
tinify.from_file("unoptimized.png").to_file("optimized.png")

# Compress from URL
tinify.from_url("https://example.com/image.jpg").to_file("optimized.jpg")

# Compress from buffer
source_data = b"<image data>"
tinify.from_buffer(source_data).to_file("optimized.jpg")

Advanced Usage

Resizing

# Scale image to fit within 300x200px while preserving aspect ratio
tinify.from_file("original.jpg").resize(
    method="scale",
    width=300,
    height=200
).to_file("resized.jpg")

# Fit image to exact 300x200px dimensions
tinify.from_file("original.jpg").resize(
    method="fit",
    width=300,
    height=200
).to_file("resized.jpg")

# Cover 300x200px area while preserving aspect ratio
tinify.from_file("original.jpg").resize(
    method="cover",
    width=300,
    height=200
).to_file("resized.jpg")

Format Conversion

# Convert to WebP format
tinify.from_file("image.png").convert(
    type=tinify.Format.WEBP
).to_file("image.webp")
# Convert to smallest format
# (tinify.Format.WEBP, PNG, JPEG, JPG, AVIF, JXL and ANY)
converted = tinify.from_file("image.png").convert(
    type=[tinify.Format.WEBP, tinify.Format.AVIF, tinify.Format.JXL]
)
extension = converted.result().extension
converted.to_file("image." + extension)

The constants are plain strings, so type="image/webp" keeps working.

Compression Count Monitoring

# Check the number of compressions made this month
compression_count = tinify.compression_count
print(f"You have made {compression_count} compressions this month")

Error Handling

import tinify

tinify.key = "YOUR_API_KEY"

try:
    tinify.from_file("unoptimized.png").to_file("optimized.png")
except tinify.AccountError as e:
    # Verify or update API key
    print(f"Account error: {e.message}")
except tinify.ClientError as e:
    # Handle client errors (e.g., invalid image)
    print(f"Client error: {e.message}")
except tinify.ServerError as e:
    # Handle server errors
    print(f"Server error: {e.message}")
except tinify.ConnectionError as e:
    # Handle network connectivity issues
    print(f"Connection error: {e.message}")
except Exception as e:
    # Handle general errors
    print(f"Error: {str(e)}")

Running tests

pip install -r requirements.txt -r test-requirements.txt
py.test

To test more runtimes, tox can be used

tox

Integration tests

pip install -r requirements.txt -r test-requirements.txt
TINIFY_KEY=$YOUR_API_KEY py.test test/integration.py

License

This software is licensed under the MIT License. See LICENSE for details.

Support

For issues and feature requests, please use our GitHub Issues page or contact us at support@tinify.com

Metadata

Release files for tinify 1.8.0

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

Source distribution (sdist)

Source distribution for tinify 1.8.0
File Size Uploaded Provenance Actions
tinify-1.8.0.tar.gz 125.4 kB Without provenance Details for tinify-1.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tinify 1.8.0
File Interpreter ABI Platform Actions
tinify-1.8.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for tinify-1.8.0-py2.py3-none-any.whl

Total release size: 250.0 kB

Release files / tinify-1.8.0.tar.gz

Download URL tinify-1.8.0.tar.gz
Size 125.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b1af4d953b62f8cb2750f6e40ff517ce45f17e6ef5e7f3869c60e9175f449759
BLAKE2b-256 checksum
How to use checksums
785021416384eaa9408b09c4d261ea5e8b2cc1fb04be542b027f0ef96739373a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / tinify-1.8.0-py2.py3-none-any.whl

Download URL tinify-1.8.0-py2.py3-none-any.whl
Size 124.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7cd8bfe28d29393b834c9742cba8c2936b69253d29cec50516175972a5d93e91
BLAKE2b-256 checksum
How to use checksums
e4aedfa92e9f931f26d51c8fa54f0164cd6c968533a2adf107ea475b6357d94f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for tinify 1.8.0

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