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.