简体   繁体   中英

How do I create a user given amount of images (in PIL - Python)?

I have a problem that should be easy to solve but I can't think of how to do it. Here is my code:

image_1 = Image.new('RGB', (w//2, h//2), (255, 255, 255))
image_2 = Image.new('RGB', (w//2, h//2), (255, 255, 255))
image_3 = Image.new('RGB', (w//2, h//2), (255, 255, 255))
image_4 = Image.new('RGB', (w//2, h//2), (255, 255, 255))
image_5 = Image.new('RGB', (w//2, h//2), (255, 255, 255))
image_6 = Image.new('RGB', (w//2, h//2), (255, 255, 255))
image_7 = Image.new('RGB', (w//2, h//2), (255, 255, 255))
image_8 = Image.new('RGB', (w//2, h//2), (255, 255, 255))

However, I want to do this (create images and name them) a user given amount of times. How is this possible? Full code is here - https://github.com/LouisPi/flashcard_generator/blob/master/main.py

Use a loop or list comprehension. Something like:

images = []
for i in range(num_images):
    images.append(Image.new('RGB', (w//2, h//2), (255, 255, 255)))

Or for the comprehension:

images = [Image.new('RGB', (w//w, h//w), (255, 255, 255)) for i in range(num_images)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM