Home  >  Article  >  Backend Development  >  Python uses Guetzli to batch compress images example code

Python uses Guetzli to batch compress images example code

高洛峰
高洛峰Original
2017-03-24 16:16:291828browse

This article mainly introduces Python's use of Guetzli to batch compress images. It introduces Google's open source image compression tool Guetzli in detail. It is of great practical value and friends in need can refer to it.

Google has open sourced again, this time an image algorithm tool called Guetzli. Guetzli, which means "cookie" in Swiss German, is a JPEG encoder for digital images and web images that can achieve a faster online experience by producing smaller JPEG files while maintaining the same Compatibility with current browsers, image processing applications, and JPEG standards. Google says Guetzli creates high-quality JPEG image files that are 35 percent smaller than current compression methods.

Today I played with Google’s open source image compression tool Guetzli and found that the compression effect of a single image is still good, so I wrote a simple python script to compress images in batches

Prerequisite Conditions

1. After Guetzli is installed, you can use the command line tool

2.Python environment

Simple code

# -*- coding: utf-8 -*-

import os

dir_name = "你选择的图片文件夹"


def get_file_name(file_dir):
  for root, dirs, files in os.walk(file_dir):
    print(root)
    # print(dirs)
    # print(files)
  return files


files = get_file_name(dir_name)
print(files)
os.chdir(dir_name)
for file in files:
  cmd = "guetzli --quality 85 --verbose " + file + " " + file
  os.system(cmd)

Effect

Before compression

Python uses Guetzli to batch compress images example code

After compression

Python uses Guetzli to batch compress images example code

The above is the detailed content of Python uses Guetzli to batch compress images example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn