图1:python随机创建图片(可设置图片大小和颜色)
一、案例:
需要随机生成多个大小不等,颜色随机的照片,如何用python快速生成生成图片,可设置图片生成个数,图片大小以及颜色随机呢?请看以下代码:
二、python代码:
from PIL import Image, ImageDraw
import random
import os
def generate_random_image(output_folder, image_count):
# Ensure the output folder exists
os.makedirs(output_folder, exist_ok=True)
for i in range(image_count):
# Generate random width and height for the image
width = random.randint(100, 500)
height = random.randint(100, 500)
# Generate a random color for the image
color = (random.randint(0, 255), random.randint(0, 255), random.randi