了解国旗的相关知识和五星红旗的相关数据。利用Python语言绘制五星红旗。要求旗面宽度为1200像素。根据所学到的五星红旗的知识,计算出五颗星的准确位置、大小和方向。要求绘图完成后隐藏绘图海龟,程序不得自动退出。绘图窗口标题为“某某某心中的五星红旗”。
时间: 2025-06-22 08:37:10 浏览: 10
五星红旗是中国的象征,由红色底色和五个黄色五角星组成。在Python中,我们可以通过`PIL`(Pillow)库来创建和绘制国旗。以下是一个基本的示例,展示了如何实现这个需求:
```python
from PIL import Image, ImageDraw, ImageFont
# 设定国旗尺寸
flag_width = 1200
flag_height = int(flag_width * 0.79) # 红色底占79%
# 创建红底图像
img = Image.new('RGB', (flag_width, flag_height), color='red')
draw = ImageDraw.Draw(img)
# 计算五星的位置和大小
star_radius = int(flag_height / 10) # 假设每个星星直径为旗高十分之一
x1, y1 = flag_width / 2 - star_radius, flag_height - star_radius
x2, y2 = x1 + 2 * star_radius, y1 + star_radius
x3, y3 = x1, y1 + star_radius
x4, y4 = x1 - star_radius, y1 - star_radius
# 绘制五星(五角星通常有5个点)
points = [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y4)]
draw.polygon(points, fill='gold') # 黄色
# 画第一颗星,位于左上角
x5, y5 = 50, 50 # 任意相对较小的位置,这里简化了计算
draw.polygon([(x5, y5), (x5+star_radius, y5+star_radius),
(x5+star_radius*2, y5-star_radius), (x5, y5-star_radius*2),
(x5-star_radius, y5-star_radius)], fill='gold')
# 设置字体和文字
font = ImageFont.truetype("arial.ttf", size=50)
text = "某某某心中的五星红旗"
text_color = 'white'
text_position = (50, flag_height - 50)
# 添加国名
draw.text(text_position, text, font=font, fill=text_color)
# 隐藏绘图海龟
del draw
# 展示图片但不自动关闭窗口
img.show()
阅读全文
相关推荐


















