Python converts images in batches from png to WebP,

Source: Internet
Author: User
Tags glob webp

Python converts images in batches from png to WebP,

Effect

Will be located in/img1000 images in the directory.pngImage, convert.webpFormat.img_webpFolder.


Source image directory


Target Image directory

For how to generate 1000 images in batches, refer to this article: generate images of any size in batches using Python.

Implementation example

import globimport osimport threadingfrom PIL import Imagedef 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 += 1if __name__ == "__main__":  start()

Note:This project needs to be referencedPILLibrary.

Considering the large number of linear-intensive operations, multithreading concurrency is used. Passthreading.Thread()Note the following when creating a thread object,argsThe parameter only accepts the ancestor.

Here, we useImage.open()Function to open the image.

Final callsave("img_webp/webp_" + str(index) + ".webp", "WEBP")Method to write data to the specified location in the specified format. WhereformatThe parameter is in the target format.

Now, the content of this article is almost over. Have you learned it? I hope this will help you in your study and work.

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.