Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python client for the Tinify API.

License

NotificationsYou must be signed in to change notification settings

tinify/tinify-python

Repository files navigation

MIT LicenseCIPyPIPyPI - Python VersionPyPI - Wheel

Tinify API client for Python

Tinify is the official Python client for theTinyPNG andTinyJPG image compression API, enabling developers to intelligently compress, resize, convert and optimize PNG, APNG, JPEG, WebP and AVIF images programmatically. Read more athttps://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

importtinify# Set your API key (get one for free at https://tinypng.com/developers)tinify.key="YOUR_API_KEY"# Compress an image from a filetinify.from_file("unoptimized.png").to_file("optimized.png")# Compress from URLtinify.from_url("https://example.com/image.jpg").to_file("optimized.jpg")# Compress from buffersource_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 ratiotinify.from_file("original.jpg").resize(method="scale",width=300,height=200).to_file("resized.jpg")# Fit image to exact 300x200px dimensionstinify.from_file("original.jpg").resize(method="fit",width=300,height=200).to_file("resized.jpg")# Cover 300x200px area while preserving aspect ratiotinify.from_file("original.jpg").resize(method="cover",width=300,height=200).to_file("resized.jpg")

Format Conversion

# Convert to WebP formattinify.from_file("image.png").convert(type=["image/webp"]).to_file("image.webp")
# Convert to smallest formatconverted=tinify.from_file("image.png").convert(type=["image/webp","image/webp"])extension=converted.result().extensionconverted.to_file("image."+extension)

Compression Count Monitoring

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

Error Handling

importtinifytinify.key="YOUR_API_KEY"try:tinify.from_file("unoptimized.png").to_file("optimized.png")excepttinify.AccountErrorase:# Verify or update API keyprint(f"Account error:{e.message}")excepttinify.ClientErrorase:# Handle client errors (e.g., invalid image)print(f"Client error:{e.message}")excepttinify.ServerErrorase:# Handle server errorsprint(f"Server error:{e.message}")excepttinify.ConnectionErrorase:# Handle network connectivity issuesprint(f"Connection error:{e.message}")exceptExceptionase:# Handle general errorsprint(f"Error:{str(e)}")

Running tests

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

To test more runtimes, tox can be used

tox

Integration tests

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

License

This software is licensed under the MIT License. SeeLICENSE for details.

Support

For issues and feature requests, please use ourGitHub Issues page or contact us atsupport@tinify.com


[8]ページ先頭

©2009-2026 Movatter.jp