Use Python to generate images of any size in batches,

Source: Internet
Author: User

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.

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.