Python generates images of any size in bulk

Source: Internet
Author: User
Achieve Results

From the source image, 1000 images are generated in the current working directory under the/IMG directory, from 1*1 to 1000*1000 pixels, respectively.

The effect is as follows:


Directory structure

Implementation examples

#-*-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: The project requires a reference PIL library.

Here, we use the resize function.

As with most script libraries, resize functions also support chained calls. First by resize((size, size), Image.ANTIALIAS) specifying the size and quality, where for parameter two:

Parameter values Meaning
Image.nearest Low quality
Image.bilinear Bilinear
Image.bicubic Three secondary spline interpolation
Image.antialias Quality

save("img/png_%d.png" % size)the method is eventually called to write to the specified location in the specified format.

In addition, multi-threaded concurrency is used, given the large number of linear-intensive operations.

Conclusion

The above is the use of Python batch generation of any size picture of the entire content, I hope to learn from you and use Python can be helpful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.