首页 > 编程语言 >端午安康代码PYTHON

端午安康代码PYTHON

时间:2024-06-10 15:57:34浏览次数:27  
标签:CANVAS ratio 端午 PYTHON self random 安康 points frame

import random

from math import sin, cos, pi, log

from tkinter import *

CANVAS_WIDTH = 640

CANVAS_HEIGHT = 480

CANVAS_CENTER_X = CANVAS_WIDTH / 2

CANVAS_CENTER_Y = CANVAS_HEIGHT / 2

IMAGE_ENLARGE = 11

HEART_COLOR = "#FF99CC"





def center_window(root, width, height):

    screenwidth = root.winfo_screenwidth()  # 获取显示屏宽度

    screenheight = root.winfo_screenheight()  # 获取显示屏高度

    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) /

                            2, (screenheight - height) / 2)  # 设置窗口居中参数

    root.geometry(size)  # 让窗口居中显示





def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):

    x = 16 * (sin(t) ** 3)

    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))

    # 放大

    x *= shrink_ratio
    y *= shrink_ratio
    # 移到画布中央

    x += CANVAS_CENTER_X

    y += CANVAS_CENTER_Y

    return int(x), int(y)





def scatter_inside(x, y, beta=0.15):

    ratio_x = - beta * log(random.random())

    ratio_y = - beta * log(random.random())

    dx = ratio_x * (x - CANVAS_CENTER_X)

    dy = ratio_y * (y - CANVAS_CENTER_Y)

    return x - dx, y - dy





def shrink(x, y, ratio):

    force = -1 / (((x - CANVAS_CENTER_X) ** 2 +

                  (y - CANVAS_CENTER_Y) ** 2) ** 0.6)

    dx = ratio * force * (x - CANVAS_CENTER_X)

    dy = ratio * force * (y - CANVAS_CENTER_Y)

    return x - dx, y - dy





def curve(p):

    return 2 * (2 * sin(4 * p)) / (2 * pi)





class Heart:

    def __init__(self, generate_frame=20):

        self._points = set()  # 原始爱心坐标集合

        self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合

        self._center_diffusion_points = set()  # 中心扩散效果点坐标集合

        self.all_points = {}  # 每帧动态点坐标

        self.build(2000)

        self.random_halo = 1000

        self.generate_frame = generate_frame

        for frame in range(generate_frame):

            self.calc(frame)



    def build(self, number):

        for _ in range(number):

            t = random.uniform(0, 2 * pi)

            x, y = heart_function(t)

            self._points.add((x, y))

        # 爱心内扩散

        for _x, _y in list(self._points):

            for _ in range(3):

                x, y = scatter_inside(_x, _y, 0.05)

                self._edge_diffusion_points.add((x, y))

        # 爱心内再次扩散

        point_list = list(self._points)

        for _ in range(4000):

            x, y = random.choice(point_list)

            x, y = scatter_inside(x, y, 0.17)

            self._center_diffusion_points.add((x, y))



    @staticmethod

    def calc_position(x, y, ratio):

        force = 1 / (((x - CANVAS_CENTER_X) ** 2 +

                      (y - CANVAS_CENTER_Y) ** 2) ** 0.520)

        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)

        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)

        return x - dx, y - dy



    def calc(self, generate_frame):

        ratio = 10 * curve(generate_frame / 10 * pi)

        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))

        halo_number = int(

            3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))

        all_points = []

        # 光环

        heart_halo_point = set()

        for _ in range(halo_number):

            t = random.uniform(0, 2 * pi)

            x, y = heart_function(t, shrink_ratio=11.6)

            x, y = shrink(x, y, halo_radius)

            if (x, y) not in heart_halo_point:

                heart_halo_point.add((x, y))

                x += random.randint(-14, 14)

                y += random.randint(-14, 14)

                size = random.choice((1, 2, 2))

                all_points.append((x, y, size))

        # 轮廓

        for x, y in self._points:

            x, y = self.calc_position(x, y, ratio)

            size = random.randint(1, 3)

            all_points.append((x, y, size))

        # 内容

        for x, y in self._edge_diffusion_points:

            x, y = self.calc_position(x, y, ratio)

            size = random.randint(1, 2)

            all_points.append((x, y, size))

        self.all_points[generate_frame] = all_points

        for x, y in self._center_diffusion_points:

            x, y = self.calc_position(x, y, ratio)

            size = random.randint(1, 2)

            all_points.append((x, y, size))

        self.all_points[generate_frame] = all_points



    def render(self, render_canvas, render_frame):

        for x, y, size in self.all_points[render_frame % self.generate_frame]:

            render_canvas.create_rectangle(

                x, y, x + size, y + size, width=0, fill=HEART_COLOR)





def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):

    render_canvas.delete('all')

    render_heart.render(render_canvas, render_frame)

    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)





if __name__ == '__main__':

    root = Tk()

    root.title("爱心")

    center_window(root, CANVAS_WIDTH, CANVAS_HEIGHT)  # 窗口居中显示

    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)

    canvas.pack()

    heart = Heart()

    draw(root, canvas, heart)

    Label(root, text="你想要替换的文字", bg="black", fg="#FF99CC", font="Helvetic 20 bold").place(

        relx=.5, rely=.5, anchor=CENTER)

    root.mainloop()

标签:CANVAS,ratio,端午,PYTHON,self,random,安康,points,frame
From: https://blog.csdn.net/qq490525427/article/details/139577857

相关文章

  • python爬虫笔记——学习笔记—6
    爬虫笔记——学习笔记—61.安装scrapy打开此电脑![img](file:///C:/Users/Administrator/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png在桌面的上栏目输入cmd并打开再命令框中升级python:python-mpipinstall–upgradepip安装scrapy:pipinstallscrapy安装......
  • python-数据分析-Pandas-5、DataFrame-index
    Index类型,它为Series和DataFrame对象提供了索引服务,有了索引我们就可以排序数据(sort_index方法)、对齐数据(在运算和合并数据时非常重要)并实现对数据的快速检索(索引运算)。由于DataFrame类型表示的是二维数据,所以它的行和列都有索引,分别是index和columns。Index类型的创建的比较简单......
  • 如何用python修复一张有多人图像的老照片,修复后照片是彩色高清
    要修复一张有多人图像的老照片,可以使用OpenCV库和深度学习模型。以下是一个简单的示例:```pythonimportcv2importnumpyasnpimporttensorflowastffromtensorflow.keras.modelsimportload_model#加载预训练的深度学习模型model=load_model('path/to/your/mode......
  • python常用函数
    python常用函数一、进制转换#字符串与ASCII码转换print(ord("A"))#65print(chr(65))#A#进制转换print(bin(78))#0b1001110print(oct(78))#0o116print(hex(78))#0x4eprint(int('4e',16))二、数学函数函数返回值(描述)abs(x)返......
  • 将这张图片修复为彩色高清,然后在Python中,可以使用以下库来执行上述任务
    将这张图片修复为彩色高清,然后在Python中,可以使用以下库来执行上述任务OpenCV:用于图像处理、面部检测等。dlib:用于面部检测和特征点标记。Scikit-image:用于图像处理和修复。TensorFlow或PyTorch:用于训练和使用深度学习模型,如图像修复GAN和超分辨率模型。首先,我们需要将......
  • python快速生成二维码及读取二维码内容 pyqrcode MyQR pyzbar
    目录效果图生成二维码方式1:pyqrcode方式2:MyQR  读取二维码效果图生成二维码方式1:pyqrcode安装pipinstallpyqrcode代码实现importpyqrcode#text为保存在二维码的内容。text为完整链接时,扫描后可直接跳转到该链接text='https://www.baidu.com/'qr=pyqrc......
  • 使用Python去除PNG图片背景
    要使用Python自动去除PNG图片的背景,你可以使用remove.bg的API,或者使用一些图像处理库如OpenCV和Pillow结合MaskR-CNN等深度学习模型。以下是一个使用Pillow库的简单示例:安装所需库:pipinstallpillownumpy使用以下代码去除背景:fromPILimportImageimportnumpyas......
  • Python GUI编程:深入探索现代GUI库及其创新应用
    目录引言PythonGUI库概览1.Tkinter2.PyQt/PySide3.wxPython4.Kivy5.PyGTK6.FLTK(pyFLTK)创新应用案例1.交互式数据分析工具2.智能物联网(IoT)仪表板3.增强现实(AR)辅助设计软件4.跨平台的科学计算软件5.交互式教育软件实战示例1:使用PyQt5开发智能......
  • python-数据分析-Pandas-4、DataFrame-数据透视
    经过前面的学习,我们已经将数据准备就绪而且变成了我们想要的样子接下来就是最为重要的数据透视阶段了。当我们拿到一大堆数据的时候,如何从数据中迅速的解读出有价值的信息把繁杂的数据变成容易解读的统计图表并再此基础上产生业务洞察,这就是数据分析要解决的核心问题。数据透视......
  • python-pip配置镜像源加速下载Python包安装:快速配置pip源-国内加速源
    目录一、简介二、国内常用加速源 三、配置pip加速源1.临时配置2.永久配置a.配置命令b.查看是否配置成功c.还原配置一、简介pip的原本安装源是PythonPackageIndex(PyPI)的官方源,其URL为https://pypi.org/simple/。PyPI是Python社区用来发布和共享软件包的官方仓......