当前位置: 首页 > news >正文

python语言随机人物头像图片生成器程序代码

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()
http://www.zskr.cn/news/142968.html

相关文章:

  • 知网AIGC疑似度50%正常吗?学校要求ai率低于30%?
  • 2026年河北省职业院校技能大赛移动应用设计与开发赛项样题
  • Azure 告警体系优化实践
  • 知网AIGC疑似度50%怎么办?1个降AI率工具轻松搞定,亲测好用!
  • 多个服务工作者线程是否可以共存
  • 基于 Python 的人脸+服装双重验证照片识别系统
  • 解析 ‘Bootloader’ 中的 C++ 环境初始化:从全局变量构造到堆栈指针设置全过程
  • 如何利用 C++ 实现自定义的 `operator new`:为特定组件构建高性能的片上内存分配器
  • 2025/12/23 今天学的day9的lecode的344和151
  • 基于MPC的换道五次多项式换道:Simulink与CarSim联合仿真之旅
  • 平衡树 学习笔记 - -Graphic
  • Leetcode—181. 超过经理收入的员工Q2. 超过经理收入的员工【简单】
  • 推行无纸化审图,国产CAD助力企业降本增效与绿色办公
  • 35、SharePoint开发:架构、特性与部署全解析
  • 告别 Win10 服务器开机漫长 fix!系统盘必检 + 外挂盘精准跳过实操全攻略
  • 打造团队专属的测试效能平台:2025年低代码/零代码在测试工具中的应用
  • C8精准识人:为什么优秀的员工,是不需要管的
  • OpenEuler 等 Linux 系统中运行 Vue 项目的方法
  • API测试进阶:基于契约测试(Pact)保障微服务间数据一致性的完整方案
  • 宁波效果图可靠之选,半条鱼设计公司如何?
  • AI便民就医系统:用技术重构就医全流程
  • step-audio-2 企业级接入全攻略:从配置到运维
  • 2025国内最新水地源热泵厂家 TOP5 评测!山东临沂等地区优质品牌权威榜单发布,引领绿色暖通空调新生态 - 全局中转站
  • 毕业季必看!研究生必备的高效论文大纲模版指南
  • 2025年评价高的空调安装公司有哪些?主要有那些受欢迎的品牌? - 讯息观点
  • MonkeyCode:让AI编程助手真正成为你的“私有“生产力工具
  • rknn模型部署技巧
  • FT8433 低成本5V80mA非隔离BCUK方案直接替代KP311ALGA
  • 43、深入探索文件属性与图像操作
  • 44、WinRT 中的图像、音频和视频处理