import os
import random
from PIL import Image
# 新建一个223的文件夹放到D盘根目录,223里放上要改800尺寸的n张图片,运行脚本,会在d
# 盘生成一个333的文件夹,里边就是800*800(具体尺寸自己改,后缀也可以自己改)的图片
file_path = r"d:\223" # 原始图像路径
raw_files = os.walk(file_path) # 遍历所有图像
width, height = 800, 800 # 修改后的图像尺寸大小
save_path = r"d:\333" # 修改后图像存储的路径
if not os.path.exists(save_path): # 如果没有这个文件夹,就新建
os.makedirs(save_path)
for root, dirs, files in raw_files:
for file in files: # 展现各文件
picture_path = os.path.join(root, file) # 得到图像的绝对路径
pic_org = Image.open(picture_path) # 打开图像
pic_new = pic_org.resize((width, height), Image.ANTIALIAS) # 图像尺寸修改
pic_new_path = os.path.join(save_path, file) # 新图像存储绝对路径
pic_new.save(pic_new_path) # 存储文件
# 随机根据RGB生成图片
img = Image.new(mode='RGB', size=(800, 800), color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
# img.show() # 在图片查看器中打开
with open('aaa.jpg', 'wb') as f: # 保存在本地
img.save(f, format='jpeg')