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语言随机人物头像图片生成器程序代码
张小明
前端开发工程师
Azure 告警体系优化实践
背景 在云原生架构中,告警系统是保障服务稳定性的关键。然而,不合理的告警阈值会导致两个极端问题: 阈值过低:频繁告警,造成告警疲劳 阈值过高:无法及时发现问题 本文记录一次全面的 Azure 告警优化实践,涵盖 Container Apps、AI Foundry、API Management 等服务。 优…
论文AI率超过学校要求,怎么把论文AIGC疑似度降到20%?
2025年起,高校已明确要求毕业论文要检测AIGC率,AI率高于30%或40%就不能参加答辩,而部分学校、硕士论文更加严格,要求在20%以内。 这其中,大多数高校使用的AIGC检测系统是知网、万方、维普等主流查重系统,这…
降低知网AIGC疑似度的万能公式:比话降AI+手动调整。
2025年起,高校已明确要求毕业论文要检测AIGC率,AI率高于30%或40%就不能参加答辩,而部分学校、硕士论文更加严格,要求在20%以内。 这其中,大多数高校使用的AIGC检测系统是知网、万方、维普等主流查重系统,这…
ArcGIS大师之路500技---045最小外接矩形
文章目录前言一、需求说明二、操作步骤总结前言 本文介绍在 ArcMap 软件中,为面图层绘制最小外接矩形、最小外接圆等几何图形的方法。 一、需求说明 首先说明本文要实现的需求:现有一个面要素图层,其中包含多个面要素,如下图所示…
Pydantic-DeepAgents:基于 Pydantic-AI 的轻量级生产级 Agent 框架
DeepAgents的灵感源自 LangChain deepagents,但在设计上更做减法,它强调类型安全且内置了 Docker 沙箱 2025 年的Autonomous AI Agents早就不是实验室里的花架子了。在现实世界的自动化流程、代码生成工具、数据管道以及各类智能助手中都能看到它们的身…
gige vision协议工业相机采图及存储实现,gvcp相机控制通道实现,gvsp流通道实现
gige vision协议工业相机采图及存储实现,gvcp相机控制通道实现,gvsp流通道实现,千兆以太网实现以及ddr3控制器实现。 代码已经用于实际工程,纯Verilog开发实现。工业相机的千兆以太网协议栈开发就像搭乐高积木,每个模块…