Home  >  Article  >  Backend Development  >  Use Python to batch generate images of any size

Use Python to batch generate images of any size

高洛峰
高洛峰Original
2017-01-14 13:17:381354browse

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:

Use Python to batch generate images of any size

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:

Use Python to batch generate images of any size

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn