Python converts pictures in batches from PNG format to WEBP format _python

Source: Internet
Author: User
Tags glob webp

Implementation effect

Convert the /img 1000 pictures that are located in the directory .png into a format and store them in a .webp img_webp folder.


SOURCE Picture Catalog


Target Picture Directory

About batch generation of 1000 pictures, you can refer to this article: the use of Python batch generation of any size picture

implementation example

Import glob
import OS
import threading

from PIL import Image


def create_image (infile, index):
  Os.path.splitext (infile)
  im = Image.open (infile)
  im.save ("Img_webp/webp_" + str (index) + ". Webp", "WEBP")


def start ():
  index = 0 for
  infile in Glob.glob ("Img/*.png"):
    t = Threading. Thread (Target=create_image, args= (infile, Index,))
    T.start ()
    t.join ()
    index = 1


if __name__ = " __main__ ":
  start ()

Note: This project requires a reference PIL library.

Multithreading concurrency is used in view of the large number of linear-intensive operations. threading.Thread()when you create a thread object, note that the args parameter only accepts meta ancestor.

Here we use the Image.open() function to open the image.

The final calling save("img_webp/webp_" + str(index) + ".webp", "WEBP") method to write to the specified location in the specified format. Where format the parameter is the target format.

Well, the content of this article to this is basically over, we have learned? Hope for everyone's study and work can have certain help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.