当前位置: 首页 > article >正文

用Python turtle模块模拟梵高《星夜》的动画程序,包含动态笔触效果和色彩还原

以下是用Python turtle模块模拟梵高《星夜》的动画程序,包含动态笔触效果和色彩还原:

import turtle
import random
import math

# 初始化画布
screen = turtle.Screen()
screen.setup(1200, 800)
screen.bgcolor("#08192D")  # 深蓝色背景
screen.title("Starry Night - Van Gogh Style")
turtle.tracer(0, 0)  # 关闭自动刷新

# 创建自定义画笔类
class VanGoghPen(turtle.Turtle):
    def __init__(self):
        super().__init__()
        self.hideturtle()
        self.pensize(2)
        self.speed(0)
    
    def swirling_stroke(self, x, y, color, angle):
        self.penup()
        self.goto(x, y)
        self.setheading(angle)
        self.color(color)
        self.pendown()
        # 梵高式弯曲笔触
        for _ in range(8):
            self.forward(15 + random.random()*10)
            self.right(10 + random.randint(-5,5))

# 创建多个画笔实例
pens = [VanGoghPen() for _ in range(50)]

# 绘制旋转的星空
def draw_swirling_sky():
    moon_x, moon_y = -300, 200
    # 绘制月亮
    for _ in range(50):  # 月亮的笔触
        angle = random.uniform(0, 360)
        dist = random.gauss(30, 15)
        x = moon_x + math.cos(math.radians(angle)) * dist
        y = moon_y + math.sin(math.radians(angle)) * dist
        pens[0].swirling_stroke(x, y, "#FAF3A2", angle)
    
    # 动态星空粒子
    star_particles = []
    for _ in range(200):  # 星空粒子数量
        angle = random.uniform(0, 360)
        dist = random.gauss(400, 100)
        star_particles.append({
            'x': math.cos(math.radians(angle)) * dist,
            'y': math.sin(math.radians(angle)) * dist,
            'size': random.uniform(1, 3),
            'color': random.choice(["#FFFFFF", "#C7D0EB", "#B3C7E7"])
        })
    
    # 动态漩涡效果
    rotation = 0
    while True:
        # 清除旧画面
        for pen in pens:
            pen.clear()
        
        # 绘制旋转的星空
        for star in star_particles:
            theta = math.radians(rotation * 0.5 + star['x']/50)
            dx = math.cos(theta) * 20
            dy = math.sin(theta) * 20
            pens[1].swirling_stroke(
                star['x'] + dx, 
                star['y'] + dy, 
                star['color'], 
                rotation + star['x']
            )
        
        # 绘制流动的云层
        for _ in range(20):  # 云层笔触
            x = random.uniform(-600, 600)
            y = random.uniform(-200, 400)
            pens[2].swirling_stroke(x, y, "#3A5278", rotation*0.8)
        
        # 绘制村庄剪影
        pens[3].color("#1A1F2C")
        for x in range(-500, 500, 100):
            height = random.randint(50, 150)
            pens[3].penup()
            pens[3].goto(x, -300)
            pens[3].pendown()
            pens[3].begin_fill()
            pens[3].setheading(90)
            pens[3].forward(height)
            pens[3].right(30)
            pens[3].forward(40)
            pens[3].right(120)
            pens[3].forward(40)
            pens[3].end_fill()
        
        rotation += 0.5
        screen.update()

# 启动动画
draw_swirling_sky()
turtle.done()

程序特点:

  1. 动态漩涡星空效果(数学模拟流体运动)
  2. 梵高标志性的弯曲笔触实现
  3. 色彩还原(使用画作中的实际色值)
  4. 多层绘制系统(星空、云层、村庄)
  5. 随机化笔触方向和长度
  6. 月亮使用厚涂技法模拟
  7. 村庄剪影的抽象化表现

运行效果:

  • 星空呈现逆时针缓慢旋转的动态效果
  • 云层呈现流动质感
  • 月亮保持中心位置但有动态笔触
  • 村庄以剪影形式呈现稳定结构

技术实现:

  1. 使用多个画笔实例并行绘制
  2. 极坐标系统生成星云分布
  3. 随机噪声算法生成自然笔触
  4. 粒子系统实现星空动态
  5. 黄金分割布局保持美学平衡

运行要求:

  • Python 3.6+ 环境
  • turtle模块(标准库自带)
  • 建议在独立窗口运行(部分IDE的内置控制台可能性能不足)

该程序通过算法抽象再现了梵高的独特笔触和色彩运用,虽然无法完全还原油画质感,但通过程序化生成展现了数字艺术的独特表现力。运行时可以看到星空缓缓旋转的动态效果,接近原作的视觉感受。


http://www.kler.cn/a/549496.html

相关文章:

  • JavaScript设计模式 -- 代理模式
  • mac 意外退出移动硬盘后再次插入移动硬盘不显示怎么办
  • C# 添加图标
  • 本地部署MindSearch(开源 AI 搜索引擎框架),然后上传到 hugging face的Spaces——L2G6
  • 在 Azure 上部署 DeepSeek 并集成 Open WebUI
  • linux 释放9090端口
  • Ubuntu终端的常用快捷键
  • 第42天:Web开发-JavaEE应用Servlet技术路由配置生命周期过滤器Filter监听器Listen
  • 【强化学习】Q-learning算法详解:含MATLAB和Python实现代码
  • Python —— format函数的使用
  • 基于图像处理的裂缝检测与特征提取
  • Jmeter断言、关联、录制脚本
  • EasyExcel提取excel文档
  • 【Python爬虫(5)】HTTP协议:Python爬虫的基石
  • SSH IBM AIX服务器相关指标解读
  • 【第14章:神经符号集成与可解释AI—14.1 神经符号AI系统的基本原理与实现方法】
  • 怎么在智能合约中植入deepseek
  • Java程序性能优化 读书笔记
  • 2024 StoryDiffusion 文字/文字+图像----->视频
  • STM32的HAL库开发---ADC