如何用python随机创建图片

2025-05-12 14:57:42

from PIL import mage,ImageDraw
import random
import os
def generate random image(output folder, image count):#Ensure the output folder existsos.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 imagecolor =(random,randint(0,255),random,randint(0,255),random,randint(0, 255))
#Create a new image with the specified width, height, and colorimage = Image.new("RGB",(width,height),color)
# Create a draw object to add text to the imagedraw = ImageDraw.Draw(image)
#Generate a random name for the image
image name = f"random image fi}.png
#Save the image to the output folder
image path = os.path.join(output folder, image name)
image.save(image path)
print(f"Generated random image: {image path}")
if.mainname# Specify the output folder and the number of images to generateoutput folder =rc:#随机创建图片的存放地址image count=5
#定义随机创建图片数
# Generate random imagesgenerate random image(output folder,image count)

发表评论:

Powered by PHP 学习者(mail:517730729@qq.com)

原百度博客:http://hi.baidu.com/ssfnadn

备案号:闽ICP备17000564号-1

开源中国 PHPCHINA