人物画完整版代码python
时间: 2024-12-15 21:12:26 浏览: 2
在Python中创建一个人物画完整的代码示例涉及到一些基本的图形库,如PIL(Python Imaging Library)或更现代的matplotlib、pygame等。下面是一个简单的肖像绘制例子,使用PIL库:
```python
from PIL import Image, ImageDraw
# 创建一个新的空白图片
img_width = 500
img_height = 700
background_color = (255, 255, 255) # 白色背景
image = Image.new('RGB', (img_width, img_height), background_color)
# 创建一个绘图工具
draw = ImageDraw.Draw(image)
# 定义眼睛、鼻子和嘴巴的位置和形状
eye_size = 20
mouth_width = 60
nose_width = 40
# 绘制眼睛
eyebrow_start = (img_width / 2 - eye_size * 0.8, img_height * 0.6)
eyebrow_end = (img_width / 2 + eye_size * 0.8, img_height * 0.6)
eye_left = (eyebrow_start[0] - eye_size, eyebrow_start[1])
eye_right = (eyebrow_end[0] + eye_size, eyebrow_end[1])
draw.ellipse((eye_left, (eyebrow_end[1], eyebrow_start[1])), fill=(0, 0, 0)) # 左眼
draw.ellipse(((eyebrow_start[0], eyebrow_end[1]), (eyebrow_end[0], eyebrow_start[1])) + (eye_size, eye_size), fill=(0, 0, 0)) # 右眼
# 绘制鼻子
nose_mid = (img_width / 2, img_height * 0.4)
nose_top = (nose_mid[0] - nose_width / 2, nose_mid[1] - nose_width)
nose_bottom = (nose_mid[0] + nose_width / 2, nose_mid[1] + nose_width)
draw.rectangle([(nose_top[0], nose_top[1]), (nose_bottom[0], nose_bottom[1])], fill=(0, 0, 0))
# 绘制嘴巴
mouth_left = (img_width / 2 - mouth_width / 2, img_height * 0.3)
mouth_right = (img_width / 2 + mouth_width / 2, img_height * 0.2)
mouth_top = (mouth_left[0], mouth_left[1] + mouth_width)
mouth_bottom = (mouth_right[0], mouth_right[1] - mouth_width)
draw.polygon([(mouth_left[0], mouth_top[1]), (mouth_right[0], mouth_top[1]), (mouth_right[0], mouth_bottom[1])], fill=(0, 0, 0))
# 显示图片
image.show()
阅读全文