Using Python to generate pictures of any size in batches _python

Source: Internet
Author: User

Implementation effect

From the source picture, in the current working directory in the/IMG directory to generate 1000 pieces, from 1*1 to 1000*1000 pixels of the picture.

The effect is as follows:


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 ()

Note: This project requires a reference PIL library.

Here, we use resize functions.

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

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

The final calling save("img/png_%d.png" % size) method to write to the specified location in the specified format.

In addition, multithreading concurrency is used in view of the large number of linear-intensive operations.

Conclusion

The above is the use of Python batch production of any size pictures of the entire content, I hope to learn and use Python can help.

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.