Batch generate images of any size using Python

Source: Internet
Author: User
I don't know if you have ever encountered this problem. we need 1000 images, ranging from 1x1 to 1000x1000 pixels. After searching, it was found that Python was easy to implement. I decided to use Python to implement this function. I would like to share it with you. For more information, see. 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:

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 must reference the PIL Library.

Here, we use the resize function.

Like most script libraries, the resize function also supports chained calling. First, use resize (size, size), Image. ANTIALIAS) to specify the size and quality. for parameter 2:

Finally, the save ("img/png_mongod.png" % size) method is called to write data to the specified location in a 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.

For more articles about batch generation of images of any size using Python, refer to PHP Chinese network!

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.