Home > Article > Backend Development > Use Python to batch generate images of any size
Achieve effect
Use the source image to generate 1000 images ranging from 1*1 to 1000*1000 pixels in the /img directory of the current working directory.
The effect is as follows:
Implementation example
# -*- coding: utf-8 -*- import threading from PIL import Image image_size = range(1, 1001) def start(): for size in image_size: t = threading.Thread(target=create_image, args=(size,)) t.start() def create_image(size): pri_image = Image.open("origin.png") pri_image.resize((size, size), Image.ANTIALIAS).save("img/png_%d.png" % size) if __name__ == "__main__": start()
Note: This project requires Reference the PIL library.
Here, we use the resize function.
Like most script libraries, the resize function also supports chain calls. First specify the size and quality through resize((size, size), Image.ANTIALIAS), where for parameter two:
Finally call save("img/png_%d .png" % size) method, writes the specified location in the specified format.
In addition, considering that it is a large number of linearly intensive operations, multi-thread concurrency is used.
Conclusion
The above is the entire content of using Python to batch generate images of any size. I hope it will be helpful for everyone to learn and use Python.
For more articles related to using Python to batch generate images of any size, please pay attention to the PHP Chinese website!