当前位置: 首页 > 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.jsqmd.com/news/130295/

相关文章:

  • 知网AIGC疑似度50%正常吗?学校要求ai率低于30%?
  • AI元人文与岐金兰:一场范式革命的孤独旅程与文明意义
  • 2026年河北省职业院校技能大赛移动应用设计与开发赛项样题
  • 解析 ‘Memory-mapped I/O’ (MMIO):如何通过 C++ 结构体映射硬件寄存器实现高效驱动开发?
  • Azure 告警体系优化实践
  • 在看完近50篇 VLA+RL 工作之后......
  • 多个服务工作者线程是否可以共存
  • 知网AIGC疑似度50%怎么办?1个降AI率工具轻松搞定,亲测好用!
  • Oracle sql tuning guide 翻译 Part 6-5 --- Hint使用报告的操作优秀的方法和例子
  • 基于 Python 的人脸+服装双重验证照片识别系统
  • 什么是 ‘Linker Scripts’ (链接脚本)?控制 C++ 段(.text, .data, .bss)在物理内存中的布局
  • 有什么好用的降AIGC疑似度工具,知网AI率90%!
  • SPSS——“Kaplan-Meier生存分析”
  • 手持雷达流速仪在应急场景监测中的应用与实践
  • Pydantic-DeepAgents:基于 Pydantic-AI 的轻量级生产级 Agent 框架
  • CLRNet车道线检测模型在Orin Nano Super开发板上的全栈部署与优化实践
  • 解析 ‘Bootloader’ 中的 C++ 环境初始化:从全局变量构造到堆栈指针设置全过程
  • 智能逗狗神器方案开发,狗狗跳跳球MCU方案设计
  • 降低知网AIGC疑似度最有效方法!AI率从100%到5%!
  • 如何利用 C++ 实现自定义的 `operator new`:为特定组件构建高性能的片上内存分配器
  • 2025国内最新中央空调服务商top10测评!山东临沂等地区优质品牌权威榜单发布,绿色能源赋能多领域温控生态 - 全局中转站
  • 零模就这样吧
  • 40、SharePoint 2010及相关工具安装与站点集创建指南
  • 2025激光切割机厂家,专业国内激光切割机厂家综合榜单 - 栗子测评
  • 2025激光切割机厂家,专业国内激光切割机厂家综合榜单 - 栗子测评
  • step-audio-2 接入实战指南:从入门到生产部署
  • 基于MATLAB的模糊逻辑算法控制给定交叉口红绿灯系统
  • 2025/12/23 今天学的day9的lecode的344和151
  • 20251223给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-6.1】系统时解决给TF卡写入大文件破坏文件系统的问题
  • 向海康 ISC学习