This content originally appeared on Level Up Coding - Medium and was authored by koji_kanao
What is TinyPNG?
>TinyPNG uses smart lossy compression techniques to reduce the file size of your WEBP, JPEG, and PNG files. By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. The effect is nearly invisible but it makes a very large difference in file size!
TinyPNG has a web app that allows us to compress images(up to 20 images & max 5MB each). That is useful but we sometimes need to compress more than 20 images.
The great thing is that TinyPNG has API and packages for multiple languages.
TinyPNG Developer API
https://tinypng.com/developers
In this post, I will show you how to use the API with python.
Step1. GET API Key
Step2. Install pip package
Step3. Write a script (less 20 lines)
Step4. Run the script
Step1. GET API Key
Go to https://tinypng.com/developers and type your name and email address. You will get the API key easily.
API is free for up to 500 requests.
Step2. Install pip package
pip install tinify
Step3. Write a script
Adding try-except is good ?
ref https://tinypng.com/developers/reference/python
The script is super simple. create an src for source images and dist for optimized images.
Get file names by `glob` and pass image files to TinyPng API with tinify.from_file method.
You can pass an image file as buffer or image URL instead of an image file path.
with open(“unoptimized.jpg”, ‘rb’) as source:
source_data = source.read()
result_data = tinify.from_buffer(source_data).to_buffer()
source = tinify.from_url(“https://tinypng.com/images/panda-happy.png")
source.to_file(“optimized.png”)
app.py
import tinify
from glob import glob
import os.path
tinify.key = “your_api_key”
source_dir_name = ‘src’
destination_dir_name = ‘dist’
# get all files names in directory
files = glob(source_dir_name + ‘/*’)
# compress all files
for file in files:
print(‘compressing ‘ + file)
source = tinify.from_file(file)
file_name, ext = os.path.splitext(file)
file_name = file_name.replace(source_dir_name + ‘/’, ‘’)
source.to_file(destination_dir_name + “/” + file_name + “.png”)
print(‘compressed all images’)
Step4. Run the script
Before running the script, we need 2 small things.
First, create two directories(src and dist). If you don’t like these dir names, you can change whatever you like.
$ mkdir src dist
Then, moving image files you want to compress to `src` dir.
Almost there!
Finally, run the script!
$ python app.py
compressing src/MoreToggles.css.png
compressing src/CSSscrolshadows.jpg
compressing src/CSStoTailwindCSS.png
compressing src/broider.png
compressing src/Tailblocks.jpg
compressing src/calcolor.jpg
compressing src/screenshot-rocks.png
compressing src/SmoothShadow.png
compressing src/Neumorphism.io.png
compressed all images
repo
https://koji/tinypng_image_compress
Generally, you can reduce 20–50% of an image file size.
If you like to use more functionality easily, I recommend you to check this article.
Compress images with 16 lines of python code and TinyPNG API was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by koji_kanao
koji_kanao | Sciencx (2021-09-17T16:14:05+00:00) Compress images with 16 lines of python code and TinyPNG API. Retrieved from https://www.scien.cx/2021/09/17/compress-images-with-16-lines-of-python-code-and-tinypng-api/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.