I want to create multiple random images with PIL, using a loop function, and I want to save them with their respective indices (eg: image00.jpg, image01.jpg, image02.jpg, etc). When I run the code, all images are created on one platform, and saved in one file. Where is the problem? Thanks.
L_size=[]
M_loc=[]
M_size=[]
for n in range(10):
draw.line((100, 1400, 11990, 1400), fill=(255,255, 255), width=50)
nlv=random.randrange(4,9)
nmv=random.randrange(6,13)
for L in range (0,nlv):
lvl=random.randrange(1374,9616)
L_loc.append(lvl)
lvs=random.randrange (171,221)
L_size.append(lvs)
lx1=lvl-(lvs/2)
lx2=lvl+(lvs/2)
ly1=1400-(4*lvs)
draw.ellipse((lx1,ly1,lx2,1400), fill=(255,255, 255), outline=(255,255, 255))
for M in range (0,nmv):
mvl=random.randrange(1374,9616)
M_loc.append(mvl)
mvs=random.randrange(60,101)
M_size.append(mvs)
mx1=lvl-(mvs/2)
mx2=lvl+(mvs/2)
my1=1400-(4*mvs)
draw.ellipse((mx1,my1,mx2,1400), fill=(255,255, 255), outline=(255,255, 255))
im.save('desktop/pic.jpg', quality=100)
L_loc=[]
L_size=[]
M_loc=[]
M_size=[]
The problem with your code is you save all images in the same file. you should change the image path for each image:
L_size=[]
M_loc=[]
M_size=[]
for n in range(10):
draw.line((100, 1400, 11990, 1400), fill=(255,255, 255), width=50)
nlv=random.randrange(4,9)
nmv=random.randrange(6,13)
for L in range (0,nlv):
lvl=random.randrange(1374,9616)
L_loc.append(lvl)
lvs=random.randrange (171,221)
L_size.append(lvs)
lx1=lvl-(lvs/2)
lx2=lvl+(lvs/2)
ly1=1400-(4*lvs)
draw.ellipse((lx1,ly1,lx2,1400), fill=(255,255, 255), outline=(255,255, 255))
for M in range (0,nmv):
mvl=random.randrange(1374,9616)
M_loc.append(mvl)
mvs=random.randrange(60,101)
M_size.append(mvs)
mx1=lvl-(mvs/2)
mx2=lvl+(mvs/2)
my1=1400-(4*mvs)
draw.ellipse((mx1,my1,mx2,1400), fill=(255,255, 255), outline=(255,255, 255))
im.save(f'desktop/pic{n}.jpg', quality=100) # This will solve your problem!
L_loc=[]
L_size=[]
M_loc=[]
M_size=[]
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.