DEV Community

Cover image for Compress Images with Python(16 lines) and TinyPNG API
Koji (he/him)
Koji (he/him)

Posted on

Compress Images with Python(16 lines) and TinyPNG API

What is TinyPNG?

https://tinypng.com/

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 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 up to 500 requests.
Alt Text

Step2. Install pip package

pip install tinify
Enter fullscreen mode Exit fullscreen mode

Step3. Write a script

Adding try except is good 😜

ref https://tinypng.com/developers/reference/python

The script is super simple. create a 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()
Enter fullscreen mode Exit fullscreen mode
source = tinify.from_url("https://tinypng.com/images/panda-happy.png")
source.to_file("optimized.png")
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

repo

GitHub logo koji / tinypng_image_compress

image compress script with tinypng api

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.

https://medium.com/acronis-design/how-to-optimize-images-by-using-macos-terminal-tinypng-cli-dd0bb8e67708

Discussion (0)