|
| 1 | +# Tinify |
| 2 | + |
| 3 | +**Tinify** is the official Python client for the [TinyPNG](https://tinypng.com) and [TinyJPG](https://tinyjpg.com) image compression API, enabling developers to optimize PNG, JPEG, and WebP images programmatically. |
| 4 | + |
| 5 | +[](https://badge.fury.io/py/tinify) |
| 6 | +[](https://github.com/tinify/tinify-python/) |
| 7 | +## Features |
| 8 | + |
| 9 | +- Compress images, reducing file size by 50-80% while preserving visual quality |
| 10 | +- Resize and crop images with smart compression |
| 11 | +- Convert between PNG, JPEG, and WebP formats |
| 12 | +- Preserve metadata (optional) |
| 13 | +- Apply visual transformations with the Tinify API |
| 14 | +- Supports asynchronous operations |
| 15 | +- Comprehensive error handling |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```python |
| 20 | +pip install tinify |
| 21 | +``` |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +```python |
| 26 | +import tinify |
| 27 | + |
| 28 | +# Set your API key (get one for free at https://tinypng.com/developers) |
| 29 | +tinify.key = "YOUR_API_KEY" |
| 30 | + |
| 31 | +# Compress an image from a file |
| 32 | +tinify.from_file("unoptimized.png").to_file("optimized.png") |
| 33 | + |
| 34 | +# Compress from URL |
| 35 | +tinify.from_url("https://example.com/image.jpg").to_file("optimized.jpg") |
| 36 | + |
| 37 | +# Compress from buffer |
| 38 | +source_data = b"<image data>" |
| 39 | +tinify.from_buffer(source_data).to_file("optimized.jpg") |
| 40 | +``` |
| 41 | + |
| 42 | +## Advanced Usage |
| 43 | + |
| 44 | +### Resizing |
| 45 | + |
| 46 | +```python |
| 47 | +# Scale image to fit within 300x200px while preserving aspect ratio |
| 48 | +tinify.from_file("original.jpg").resize( |
| 49 | + method="scale", |
| 50 | + width=300, |
| 51 | + height=200 |
| 52 | +).to_file("resized.jpg") |
| 53 | + |
| 54 | +# Fit image to exact 300x200px dimensions |
| 55 | +tinify.from_file("original.jpg").resize( |
| 56 | + method="fit", |
| 57 | + width=300, |
| 58 | + height=200 |
| 59 | +).to_file("resized.jpg") |
| 60 | + |
| 61 | +# Cover 300x200px area while preserving aspect ratio |
| 62 | +tinify.from_file("original.jpg").resize( |
| 63 | + method="cover", |
| 64 | + width=300, |
| 65 | + height=200 |
| 66 | +).to_file("resized.jpg") |
| 67 | +``` |
| 68 | + |
| 69 | +### Format Conversion |
| 70 | + |
| 71 | +```python |
| 72 | +# Convert to WebP format |
| 73 | +tinify.from_file("image.png").convert( |
| 74 | + type=["image/webp"] |
| 75 | +).to_file("image.webp") |
| 76 | +``` |
| 77 | + |
| 78 | +### Compression Count Monitoring |
| 79 | + |
| 80 | +```python |
| 81 | +# Check the number of compressions made this month |
| 82 | +compression_count = tinify.compression_count |
| 83 | +print(f"You have made {compression_count} compressions this month") |
| 84 | +``` |
| 85 | + |
| 86 | +## Error Handling |
| 87 | + |
| 88 | +```python |
| 89 | +import tinify |
| 90 | + |
| 91 | +tinify.key = "YOUR_API_KEY" |
| 92 | + |
| 93 | +try: |
| 94 | + tinify.from_file("unoptimized.png").to_file("optimized.png") |
| 95 | +except tinify.AccountError as e: |
| 96 | + # Verify or update API key |
| 97 | + print(f"Account error: {e.message}") |
| 98 | +except tinify.ClientError as e: |
| 99 | + # Handle client errors (e.g., invalid image) |
| 100 | + print(f"Client error: {e.message}") |
| 101 | +except tinify.ServerError as e: |
| 102 | + # Handle server errors |
| 103 | + print(f"Server error: {e.message}") |
| 104 | +except tinify.ConnectionError as e: |
| 105 | + # Handle network connectivity issues |
| 106 | + print(f"Connection error: {e.message}") |
| 107 | +except Exception as e: |
| 108 | + # Handle general errors |
| 109 | + print(f"Error: {str(e)}") |
| 110 | +``` |
| 111 | + |
| 112 | +## Requirements |
| 113 | + |
| 114 | +- Python 3.6+ |
| 115 | +- Requests library |
| 116 | + |
| 117 | +## Documentation |
| 118 | + |
| 119 | +For comprehensive documentation, visit [https://tinypng.com/developers/reference/python](https://tinypng.com/developers/reference/python). |
| 120 | + |
| 121 | +## License |
| 122 | + |
| 123 | +This software is licensed under the MIT License. See [LICENSE](https://github.com/tinify/tinify-python/blob/master/LICENSE) for details. |
| 124 | + |
| 125 | +## Support |
| 126 | + |
| 127 | +For issues and feature requests, please use our [GitHub Issues](https://github.com/tinify/tinify-python/issues) page. |
0 commit comments