Python batch generates images of any size

高洛峰
Release: 2017-02-27 16:50:51
Original
1966 people have browsed it

Achieve the 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:

Python batch generates images of any size
Directory structure

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()
Copy after login

Note: This project needs to reference the PIL library.

Here, we use the resize function.

Like most script libraries, the resize function also supports chained calls. First specify the size and quality through resize((size, size), Image.ANTIALIAS), where for parameter two:

Image.NEARESTLow qualityImage.BILINEARBilinearImage.BICUBIC cubic spline interpolationImage.ANTIALIASHigh Quality
parameter Value ##Meaning
Final call

save("img/png_%d.png" % size) method , writes to 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 Python batch generation of images of any size, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template