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语言随机人物头像图片生成器程序代码
张小明
前端开发工程师
什么是 ‘Linker Scripts’ (链接脚本)?控制 C++ 段(.text, .data, .bss)在物理内存中的布局
各位编程领域的同仁们,大家好!今天,我们将深入探讨一个在 C 开发,尤其是在嵌入式系统、操作系统内核或任何需要精细内存控制的场景中至关重要的主题——链接脚本(Linker Scripts)。你是否曾好奇,…
解析 ‘Bootloader’ 中的 C++ 环境初始化:从全局变量构造到堆栈指针设置全过程
各位同仁,下午好!今天,我们将深入探讨一个引人入胜且充满挑战的主题:在引导加载程序(Bootloader)中初始化 C 运行环境的全过程。这不仅仅是关于编写几行代码,而是一场关于如何从一片空白的硬件状…
降低知网AIGC疑似度最有效方法!AI率从100%到5%!
2025年起,高校已明确要求毕业论文要检测AIGC率,AI率高于30%或40%就不能参加答辩,而部分学校、硕士论文更加严格,要求在20%以内。 这其中,大多数高校使用的AIGC检测系统是知网、万方、维普等主流查重系统,这…
人工智能之数学基础 优化理论:第四章 凸函数与非凸函数
人工智能之数学基础 优化理论 第四章 凸函数与非凸函数----公式关注公众号 文章目录人工智能之数学基础 优化理论前言一、凸函数的数学定义1. 凸集(Convex Set)2. 凸函数(Convex Function)3. 严格凸 & 强凸二、凸函数的判别方…
4G与4G+路由器:技术与实际体验的差异
4G路由器,作为占据大众主流市场的产品种类之一,随着4G这一概念的普及,会逐渐失去市场吗?也许大家也会疑惑这两者的不同。本文,就从这一角度出发,来和大家聊聊这两种路由器的区别。基础差异4G路由器…
还在盲目冲业绩?亚马逊增长双引擎,先重塑内功再谈破局
亚马逊的竞争已经从流量争夺转向转化效率的深度较量,实现可持续增长,关键在于系统化结合“站内转化内功”与“站外精准引流”,构建自我强化的增长飞轮。一、内功筑基:以用户为中心的Listing重塑Listing优化的核心在于高效“对话”…