importrandomfromPILimportImage,ImageDrawclassRandomAvatarGenerator:def__init__(self,avatar_size=200):"""初始化头像生成器,默认生成200x200的头像"""self.size=avatar_size self.center=(avatar_size//2,avatar_size//2)self.bg_color=(240,240,240)# 背景色def_random_color(self):"""生成随机柔和颜色(避免过亮/过暗)"""return(random.randint(100,220),random.randint(100,220),random.randint(100,220))def_draw_face(self,draw,face_color):"""绘制随机形状的脸型(圆形/方形/椭圆形)"""face_type=random.choice(["circle","square","ellipse"])radius=self.size//3x0=self.center[0]-radius y0=self.center[1]-radius x1=self.center[0]+radius y1=self.center[1]+radiusifface_type=="circle":draw.ellipse([x0,y0,x1,y1],fill=face_color,outline=(0,0,0),width=2)elifface_type=="square":draw.rectangle([x0,y0,x1,y1],fill=face_color,outline=(0,0,0),width=2)elifface_type=="ellipse":draw.ellipse([x0-10,y0,x1+10,y1],fill=face_color,outline=(0,0,0),width=2)def_draw_eyes(self,draw):"""绘制随机样式的眼睛(大小/颜色/间距随机)"""eye_size=random.randint(15,25)eye_x_offset=random.randint(30,45)eye_y_offset=random.randint(-10,5)eye_color=(0,0,0)ifrandom.random()>0.2elseself._random_color()# 左眼left_x0=self.center[0]-eye_x_offset-eye_size//2left_y0=self.center[1]+eye_y_offset-eye_size//2left_x1=self.center[0]-eye_x_offset+eye_size//2left_y1=self.center[1]+eye_y_offset+eye_size//2draw.ellipse([left_x0,left_y0,left_x1,left_y1],fill=eye_color,outline=(0,0,0),width=1)# 右眼right_x0=self.center[0]+eye_x_offset-eye_size//2right_y0=self.center[1]+eye_y_offset-eye_size//2right_x1=self.center[0]+eye_x_offset+eye_size//2right_y1=self.center[1]+eye_y_offset+eye_size//2draw.ellipse([right_x0,right_y0,right_x1,right_y1],fill=eye_color,outline=(0,0,0),width=1)def_draw_mouth(self,draw):"""绘制随机样式的嘴巴(微笑/撇嘴/直线)"""mouth_width=random.randint(30,60)mouth_y_offset=random.randint(30,50)mouth_type=random.choice(["smile","frown","line"])x0=self.center[0]-mouth_width//2y0=self.center[1]+mouth_y_offset x1=self.center[0]+mouth_width//2ifmouth_type=="smile":draw.arc([x0,y0-10,x1,y0+10],0,180,fill=(0,0,0),width=2)elifmouth_type=="frown":draw.arc([x0,y0-10,x1,y0+10],180,360,fill=(0,0,0),width=2)else:draw.line([x0,y0,x1,y0],fill=(0,0,0),width=2)def_draw_hair(self,draw,hair_color):"""绘制随机发型(短发/长发/卷发)"""hair_type=random.choice(["short","long","curly"])face_radius=self.size//3x0=self.center[0]-face_radius-10y0=self.center[1]-face_radius-30x1=self.center[0]+face_radius+10y1=self.center[1]-face_radius+10ifhair_type=="short":draw.polygon([(x0,y1),(self.center[0],y0),(x1,y1)],fill=hair_color,outline=(0,0,0),width=2)elifhair_type=="long":draw.polygon([(x0,y1),(self.center[0],y0),(x1,y1),(x1+10,self.center[1]+face_radius),(x0-10,self.center[1]+face_radius)],fill=hair_color,outline=(0,0,0),width=2)else:# 卷发用多个椭圆模拟for_inrange(5):cx=random.randint(x0,x1)cy=random.randint(y0,y1)cr=random.randint(8,15)draw.ellipse([cx-cr,cy-cr,cx+cr,cy+cr],fill=hair_color,outline=(0,0,0),width=1)defgenerate_avatar(self,save_path="random_avatar.png"):"""生成随机头像并保存"""# 创建画布img=Image.new("RGB",(self.size,self.size),self.bg_color)draw=ImageDraw.Draw(img)# 随机选择颜色face_color=self._random_color()hair_color=random.choice([(30,30,30),(139,69,19),(0,0,0),(255,215,0)])# 绘制头像元素(顺序:头发→脸型→眼睛→嘴巴)self._draw_hair(draw,hair_color)self._draw_face(draw,face_color)self._draw_eyes(draw)self._draw_mouth(draw)# 保存头像img.save(save_path)print(f"随机头像已保存至:{save_path}")returnimgif__name__=="__main__":# 初始化生成器,可自定义头像尺寸generator=RandomAvatarGenerator(avatar_size=200)# 生成10个不同的头像(文件名区分)foriinrange(10):generator.generate_avatar(save_path=f"avatar_{i+1}.png")# 可选:显示生成的第一张头像# Image.open("avatar_1.png").show()python语言随机人物头像图片生成器程序代码
张小明
前端开发工程师
计算机专业毕业论文开题报告:研究方法写作示例与技术思路解析
写在前面:这篇文章适合谁?能解决什么问题? 这篇文章主要写给 正在准备计算机专业毕业论文开题报告的本科生,尤其是那些在撰写“研究方法”部分时,不清楚该写什么、怎么写、写到什么程度才算合格的同学。 我在实际指导开…
数据库性能优化实战手册:SQL 调优 + 架构优化全攻略
数据库性能优化实战手册:SQL 调优 + 架构优化全攻略 你是否遇到过这样的场景? 业务系统上线初期运行流畅,但随着数据量激增,查询响应时间从毫秒级飙升至数秒甚至分钟级;开发团队反复优化SQL语句,却发现性能瓶颈始终难以突破;运维人员面对高负载的数据库服务器,却找不到…
基于有限体积法(FVM)求解二维导热与对流问题
一、数学模型建立 1. 控制方程 二维稳态导热-对流方程: ∂∂x(k∂T∂x)∂∂y(k∂T∂y)q˙0\frac{\partial}{\partial x}\left(k\frac{\partial T}{\partial x}\right) \frac{\partial}{\partial y}\left(k\frac{\partial T}{\partial y}\right) \dot{q} 0∂x∂…
step-audio-2 全场景接入实战手册:从配置到落地
一、前言:step-audio-2 接入价值与文档定位 step-audio-2 作为专注于音频生成、音频理解与音频编辑的AI模型,凭借高精度的音频生成还原度、全格式音频的解析与处理能力、兼容全生态工具的特性,成为企业级音频业务智能化升级的热门选型。本文将…
2025年黑客盗走35亿美元:Synbo解读加密货币最危险的真相
如果有一天,你打开加密钱包,发现资产不是下跌,而是直接被转走了,你第一反应会是什么?很多人会下意识觉得,这种事不会发生在自己身上,或者只是个别项目“倒霉”。但说实话,到了今天&a…
GPT进化论:大模型语言与AI的迭代差异及未来应用场景解析!
一、大模型语言与AI 什么是大模型语言? 大模型语言是指使用深度学习技术构建的大型语言模型。这些模型通常具有数十亿甚至千亿级别的参数,能够理解和生成自然语言文本。大模型语言的核心是Transformer架构,它通过自注意力机制和多层神经网络…