Skip to main content

Tinify API client.

Project description

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 and AVIF 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 and AVIF 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=["image/webp"]
).to_file("image.webp")
# Convert to smallest format
converted = tinify.from_file("image.png").convert(
    type=["image/webp", "image/webp"]
)
extension = converted.result().extension
converted.to_file("image." + extension)

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

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

tinify-1.7.1.tar.gz (124.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tinify-1.7.1-py2.py3-none-any.whl (123.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file tinify-1.7.1.tar.gz.

File metadata

  • Download URL: tinify-1.7.1.tar.gz
  • Upload date:
  • Size: 124.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tinify-1.7.1.tar.gz
Algorithm Hash digest
SHA256 76307e68775ac04f8175105056d0189052ea28cfa13fc2368efdaaf04fa46a66
MD5 ffff15a017e937eaf9c361243aa903d9
BLAKE2b-256 c5d5c69b37f497c06b68759ae072ee301953165668db7dc670968ea84f889889

See more details on using hashes here.

File details

Details for the file tinify-1.7.1-py2.py3-none-any.whl.

File metadata

  • Download URL: tinify-1.7.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 123.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tinify-1.7.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 087063d8d3d5a29af5d98aeb72bc9fe6305be2091ed1221fcaf55b294971ce65
MD5 6dca3bad5259ef78fa992cf61b7f9509
BLAKE2b-256 91db0d2d6e2f46e98ec9c527e9feb2aca1629f51e41eb7a955cbc9feebb5e82a

See more details on using hashes here.

Supported by

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