Use Python to generate images of any size in batches,
Effect
The source image is used to generate 1000 images in the/img directory of the current working directory, ranging from 1*1 to 1000*1000 pixels.
The effect is as follows:
Directory structure
Implementation example
# -*- coding: utf-8 -*-import threadingfrom PIL import Imageimage_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 must be referencedPILLibrary.
Here, we useresizeFunction.
Like most script libraries,resizeFunctions also support chained calls. First passresize((size, size), Image.ANTIALIAS)Specify the size and quality. For parameter 2:
| Parameter Value |
Description |
| Image. NEAREST |
Low Quality |
| Image. BILINEAR |
Bilinear |
| Image. BICUBIC |
Cubic Spline Interpolation |
| Image. ANTIALIAS |
High Quality |
Final callsave("img/png_%d.png" % size)Method to write data to the specified location in the specified format.
In addition, considering a large number of linear-intensive operations, multithreading concurrency is used.
Conclusion
The above is the use of Python to generate all the content of images of any size in batches, hope to help you learn and use Python.