效果展示:

代码实现:


from math import cos, pi
import numpy as np
import cv2class HeartSignal:def __init__(self, frame_num=20, seed_points_num=2000, seed_num=None, frame_width=1080, frame_height=960, scale=10.1):super().__init__()self.frame_width = frame_widthself.frame_height = frame_heightself.center_x = self.frame_width / 2self.center_y = self.frame_height / 2self._points = set()  # 主图坐标点self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合self._center_diffusion_points = set()  # 中心扩散效果点坐标集合self._heart_halo_point = set()  # 光晕效果坐标集合self.frame_points = []  # 每帧动态点坐标self.frame_num = frame_numself.seed_num = seed_numself.seed_points_num = seed_points_numself.scale = scaledef heart_function(self, t, frame_idx=0, scale=5.20):"""图形方程:param frame_idx: 帧的索引,根据帧数变换心形:param scale: 放大比例:param t: 参数:return: 坐标"""trans = 3trans = 3 - (1 + self.curve(frame_idx, self.frame_num)) * 0.5  # 改变心形饱满度度的参数x = 15 * (np.sin(t) ** 3)t = np.where((pi < t) & (t < 2 * pi), 2 * pi - t, t)  # 翻转x > 0部分的图形到3、4象限y = -(14 * np.cos(t) - 4 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(trans * t))ign_area = 0.15center_ids = np.where((x > -ign_area) & (x < ign_area))if np.random.random() > 0.32:x, y = np.delete(x, center_ids), np.delete(y, center_ids)  # 删除稠密部分的扩散,为了美观# 放大x *= scaley *= scale# 移到画布中央x += self.center_xy += self.center_y# 原心形方程# x = 15 * (sin(t) ** 3)# y = -(14 * cos(t) - 4 * cos(2 * t) - 2 * cos(3 * t) - cos(3 * t))return x.astype(int), y.astype(int)def butterfly_function(self, t, frame_idx=0, scale=64):"""图形函数:param frame_idx::param scale: 放大比例:param t: 参数:return: 坐标"""# 基础函数# x = 15 * (sin(t) ** 3)# y = -(14 * cos(t) - 4 * cos(2 * t) - 2 * cos(3 * t) - cos(3 * t))t = t * pip = np.exp(np.sin(t)) - 2.5 * np.cos(4 * t) + np.sin(t) ** 5x = p * np.cos(t)y = - p * np.sin(t)# 放大x *= scaley *= scale# 移到画布中央x += self.center_xy += self.center_yreturn x.astype(int), y.astype(int)def shrink(self, x, y, ratio, offset=1, p=0.5, dist_func="uniform"):"""带随机位移的抖动:param x: 原x:param y: 原y:param ratio: 缩放比例:param p::param offset::return: 转换后的x,y坐标"""x_ = (x - self.center_x)y_ = (y - self.center_y)force = 1 / ((x_ ** 2 + y_ ** 2) ** p + 1e-30)dx = ratio * force * x_dy = ratio * force * y_def d_offset(x):if dist_func == "uniform":return x + np.random.uniform(-offset, offset, size=x.shape)elif dist_func == "norm":return x + offset * np.random.normal(0, 1, size=x.shape)dx, dy = d_offset(dx), d_offset(dy)return x - dx, y - dydef scatter(self, x, y, alpha=0.75, beta=0.15):"""随机内部扩散的坐标变换:param alpha: 扩散因子 - 松散:param x: 原x:param y: 原y:param beta: 扩散因子 - 距离:return: x,y 新坐标"""ratio_x = - beta * np.log(np.random.random(x.shape) * alpha)ratio_y = - beta * np.log(np.random.random(y.shape) * alpha)dx = ratio_x * (x - self.center_x)dy = ratio_y * (y - self.center_y)return x - dx, y - dydef curve(self, x, x_num):"""跳动周期曲线:param p: 参数:return: y"""# 可以尝试换其他的动态函数,达到更有力量的效果(贝塞尔?)def ori_func(t):return cos(t)func_period = 2 * pireturn ori_func(x / x_num * func_period)def gen_points(self, points_num, frame_idx, shape_func):# 用周期函数计算得到一个因子,用到所有组成部件上,使得各个部分的变化周期一致cy = self.curve(frame_idx, self.frame_num)ratio = 10 * cy# 图形seed_points = np.linspace(0, 2 * pi, points_num)seed_x, seed_y = shape_func(seed_points, frame_idx, scale=self.scale)x, y = self.shrink(seed_x, seed_y, ratio, offset=2)point_size = np.random.choice([1, 2], x.shape, replace=True, p=[0.5, 0.5])tag = np.ones_like(x)def delete_points(x_, y_, ign_area, ign_prop):ign_area = ign_areacenter_ids = np.where((x_ > self.center_x - ign_area) & (x_ < self.center_x + ign_area))center_ids = center_ids[0]np.random.shuffle(center_ids)del_num = round(len(center_ids) * ign_prop)del_ids = center_ids[:del_num]x_, y_ = np.delete(x_, del_ids), np.delete(y_, del_ids)  # 删除稠密部分的扩散,为了美观return x_, y_# 多层次扩散for idx, beta in enumerate(np.linspace(0.05, 0.2, 6)):alpha = 1 - betax_, y_ = self.scatter(seed_x, seed_y, alpha, beta)x_, y_ = self.shrink(x_, y_, ratio, offset=round(beta * 15))x = np.concatenate((x, x_), 0)y = np.concatenate((y, y_), 0)p_size = np.random.choice([1, 2], x_.shape, replace=True, p=[0.55 + beta, 0.45 - beta])point_size = np.concatenate((point_size, p_size), 0)tag_ = np.ones_like(x_) * 2tag = np.concatenate((tag, tag_), 0)# 光晕halo_ratio = int(7 + 2 * abs(cy))  # 收缩比例随周期变化# 基础光晕x_, y_ = shape_func(seed_points, frame_idx, scale=self.scale + 0.9)x_1, y_1 = self.shrink(x_, y_, halo_ratio, offset=18, dist_func="uniform")x_1, y_1 = delete_points(x_1, y_1, 20, 0.5)x = np.concatenate((x, x_1), 0)y = np.concatenate((y, y_1), 0)# 炸裂感光晕halo_number = int(points_num * 0.6 + points_num * abs(cy))  # 光晕点数也周期变化seed_points = np.random.uniform(0, 2 * pi, halo_number)x_, y_ = shape_func(seed_points, frame_idx, scale=self.scale + 0.9)x_2, y_2 = self.shrink(x_, y_, halo_ratio, offset=int(6 + 15 * abs(cy)), dist_func="norm")x_2, y_2 = delete_points(x_2, y_2, 20, 0.5)x = np.concatenate((x, x_2), 0)y = np.concatenate((y, y_2), 0)# 膨胀光晕x_3, y_3 = shape_func(np.linspace(0, 2 * pi, int(points_num * .4)),frame_idx, scale=self.scale + 0.2)x_3, y_3 = self.shrink(x_3, y_3, ratio * 2, offset=6)x = np.concatenate((x, x_3), 0)y = np.concatenate((y, y_3), 0)halo_len = x_1.shape[0] + x_2.shape[0] + x_3.shape[0]p_size = np.random.choice([1, 2, 3], halo_len, replace=True, p=[0.7, 0.2, 0.1])point_size = np.concatenate((point_size, p_size), 0)tag_ = np.ones(halo_len) * 2 * 3tag = np.concatenate((tag, tag_), 0)x_y = np.around(np.stack([x, y], axis=1), 0)x, y = x_y[:, 0], x_y[:, 1]return x, y, point_size, tagdef get_frames(self, shape_func):for frame_idx in range(frame_num):np.random.seed(self.seed_num)self.frame_points.append(self.gen_points(self.seed_points_num, frame_idx, shape_func))frames = []def add_points(frame, x, y, size, tag):# white = np.array([255, 255, 255], dtype='uint8')# dark_red = np.array([250, 90, 90], dtype='uint8')purple = np.array([180, 87, 200], dtype='uint8')  # 180, 87, 200light_pink = np.array([228, 140, 140], dtype='uint8')   # [228, 140, 140]rose_pink = np.array([228, 100, 100], dtype='uint8')x, y = x.astype(int), y.astype(int)frame[y, x] = rose_pinksize_2 = np.int64(size == 2)frame[y, x + size_2] = rose_pinkframe[y + size_2, x] = rose_pinksize_3 = np.int64(size == 3)frame[y + size_3, x] = rose_pinkframe[y - size_3, x] = rose_pinkframe[y, x + size_3] = rose_pinkframe[y, x - size_3] = rose_pinkframe[y + size_3, x + size_3] = rose_pinkframe[y - size_3, x - size_3] = rose_pink# frame[y - size_3, x + size_3] = color# frame[y + size_3, x - size_3] = color# 高光random_sample = np.random.choice([1, 0], size=tag.shape, p=[0.3, 0.7])# tag2_size1 = np.int64((tag <= 2) & (size == 1) & (random_sample == 1))# frame[y * tag2_size1, x * tag2_size1] = light_pinktag2_size2 = np.int64((tag <= 2) & (size == 2) & (random_sample == 1))frame[y * tag2_size2, x * tag2_size2] = purple# frame[y * tag2_size2, (x + 1) * tag2_size2] = light_pink# frame[(y + 1) * tag2_size2, x * tag2_size2] = light_pinkframe[(y + 1) * tag2_size2, (x + 1) * tag2_size2] = light_pink# frame[y * tag2_size2, x * tag2_size2] = light_pink# frame[y, x + tag2_size2] = light_pink# frame[y + tag2_size2, x] = light_pink# frame[y + tag2_size2, x + tag2_size2] = light_pinkfor x, y, size, tag in self.frame_points:frame = np.zeros([self.frame_height, self.frame_width, 3], dtype="uint8")add_points(frame, x, y, size, tag)frames.append(frame)return framesdef draw(self, wait, shape_func):frames = self.get_frames(shape_func)while True:for frame in frames:show_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)show_frame = cv2.resize(show_frame, (self.frame_width, self.frame_height))cv2.imshow("Love U", show_frame)cv2.waitKey(wait)if __name__ == '__main__':period_time = 1000 * 1.5  # 1.5s一个周期frame_num = 30wait = int(period_time / frame_num)heart = HeartSignal(frame_num=frame_num, seed_points_num=2000, seed_num=5201314, frame_width=720, frame_height=640, scale=10.1)heart.draw(wait, heart.heart_function)pass

python实现爱心代码相关推荐

  1. python动态爱心代码_python 动态绘制爱心的示例

    python 动态绘制爱心的示例 代码 import turtle turtle.bgcolor("black") turtle.pensize(2) sizeh = 1.2 de ...

  2. 如何用python画爱心代码_用 python 画爱心代码讲解

    学计算机的男生发这个给我看是什么意思?​www.zhihu.com 原理其实挺简单的. 代码网上也有. 最难的部分前人都告诉我们了, 心形可画. 要自己推导通过泰勒各种扭也可以. 通过肉眼扭我感觉也不 ...

  3. python——李询爱心❤代码

    主要用到的库为:tkinter,是Python的标准GUl库 还可以自定义画布的宽高,放大比例,心的颜色,背景颜色等 import random from math import sin, cos, ...

  4. python立体爱心代码_以下行为属于生涯发展问题的是。

    以下行为属于生涯发展问题的是. 运输包装上的标志就是指运输标志,也就是通常所说的唛头.A:对B:错 在"属性"面板的"目标"框中的_blank表示()A:将链接 ...

  5. 用python画爱心的代码-怎么用python实现画爱心

    Python中可以使用turtle库来画图,通过控制画笔运动来实现在画布上画图案. 使用Python画爱心代码如下:#!/usr/bin/env python # -*- coding:utf-8 - ...

  6. python画爱心的代码怎么运行_怎么用python实现画爱心

    Python中可以使用turtle库来画图,通过控制画笔运动来实现在画布上画图案. 使用Python画爱心代码如下:#!/usr/bin/env python # -*- coding:utf-8 - ...

  7. 《点燃我温暖你》中李峋的同款爱心代码

    前言 最近<点燃我温暖你>中李峋的爱心代码超级火,看着特别心动,这不,光棍节快到了,给兄弟们教学一波爱心代码,赶在双十一前表白,让这个双十一不在是孤单一个人! 目录 前言 Python简易 ...

  8. python爱心代码简单教程

    python爱心代码简单教程操作方法 1 将以上代码保存为.py文件,假设保存的文件名为 love.py (不会保存?先保存为txt文本,然后将后缀改为.py) 2 在终端(cmd命令窗口)输入pyt ...

  9. 情人节用python实现 跳动爱心代码 ,表白神器

    嗨害大家好鸭! 最近好像有个剧很火~ 里面是: 跳动的!!! 爱心代码!!! 简直表白利器啊!!! 用python来给大家整个福利~ 效果预览

  10. Python制作爱心跳动代码,这就是程序员的烂漫吗

    前言 最近有个剧挺火的 就是那个程序员的剧,叫什么温暖你来着 咳咳,剧我没怎么看,但是吧,里面有个爱心代码,最近可是蛮火的,今天就用Python来尝试一下吧 怎么说呢,用这个表白也可以的,万一她也看这 ...

最新文章

  1. c语言链表找姓,急啊!!!求救了 C语言编一个链表,输出姓名和学号就好
  2. java项目的逻辑结构
  3. 为什么你总是申请不到大额贷款?
  4. 【转】struct epoll_event
  5. ModelForm views.py
  6. order是mysql系统关键字_MySQL数据库如何使用“ORDER BY”关键字对查询结果进行排序呢?...
  7. [素数拓展] 质因数的个数 [2007年清华大学计算机研究生机试真题]
  8. ng-bind-html在ng-repeat中问题的解决办法
  9. WinForm实现只打开一个窗口的代码
  10. 两个摄像头合成一路_64个高空抛物摄像头安装到位 同德社区居民双手点赞
  11. php网站程序更新功能,运用PHP定时自动更新网站首页HTML的方法
  12. python hog特征提取,直接从原始图像的HoG特征提取编辑图像的HoG特征
  13. AI数据服务行业进入“认知战争”,云测数据凭什么稳居行业TOP1?
  14. 苹果雪豹操作系统正式版_如何不花一分钱用上iPhone11?更新iOS13正式版吧
  15. mysql slave是什么_是mysql表里
  16. 蔬菜干行业调研报告 - 市场现状分析与发展前景预测
  17. 化工厂人员定位保障安全管理
  18. matlab自带的插值函数interp1的四种插值方法
  19. 如何修复因卸载ccleaner导致的回收站损坏
  20. 软件ABB出现这种问题应该怎么处理呢?

热门文章

  1. redis集群原理及搭建
  2. centos7下载php7.4
  3. 计算机运行时内存会超吗,我们不曾深纠的电脑技术 篇一:我们为什么要对内存进行超频?...
  4. 更换yum源-阿里yum源
  5. kali linux安装maven
  6. pdf表格怎么转换成excel?
  7. Java二叉树的最大深度
  8. 【hightopo】【基础图标】 HT for Web简单图标的制作:进度图标
  9. El Capitan/Serial on the Intel Skylake NUC
  10. 黑苹果E3-1290 v2(ivy bridge架构)安装成功案例分享