Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 16, 2021 06:42 am GMT

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

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()
source = tinify.from_url("https://tinypng.com/images/panda-happy.png")source.to_file("optimized.png")

app.py

import tinifyfrom glob import globimport os.pathtinify.key = "your_api_key"source_dir_name = 'src'destination_dir_name = 'dist'# get all files names in directoryfiles = glob(source_dir_name + '/*')# compress all filesfor 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.pycompressing src/MoreToggles.css.pngcompressing src/CSSscrolshadows.jpgcompressing src/CSStoTailwindCSS.pngcompressing src/broider.pngcompressing src/Tailblocks.jpgcompressing src/calcolor.jpgcompressing src/screenshot-rocks.pngcompressing src/SmoothShadow.pngcompressing src/Neumorphism.io.pngcompressed all images

repo

GitHub logo koji / tinypng_image_compress

image compress script with tinypng api

tinypng_image_compress

image compress script with tinypng api

how to use

https://dev.to/kojikanao/compress-images-with-python-16-lines-and-tinypng-api-3l69

$ git clone https://github.com/koji/tinypng_image_compress.git$ cd tinypng_image_compress# move images to `src` folder$ python app.py
  • image compress
  • image resize

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


Original Link: https://dev.to/kojikanao/compress-images-with-python-16-lines-and-tinypng-api-3l69

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To