python程序代码:heart.py
from math import cos, pi
import numpy as np
import cv2
import os, glob
class HeartSignal:
def __init__(self, curve="heart", title="Love U", frame_num=20, seed_points_num=2000, seed_num=None, highlight_rate=0.3,
background_img_dir="", set_bg_imgs=False, bg_img_scale=0.2, bg_weight=0.3, curve_weight=0.7, frame_width=1080, frame_height=960, scale=10.1,
base_color=None, highlight_points_color_1=None, highlight_points_color_2=None, wait=100, n_star=5, m_star=2):
super().__init__()
self.curve = curve
self.title = title
self.highlight_points_color_2 = highlight_points_color_2
self.highlight_points_color_1 = highlight_points_color_1
self.highlight_rate = highlight_rate
self.base_color = base_color
self.n_star = n_star
self.m_star = m_star
self.curve_weight = curve_weight
img_paths = glob.glob(background_img_dir + "/*")
self.bg_imgs = []
self.set_bg_imgs = set_bg_imgs
self.bg_weight = bg_weight
if os.path.exists(background_img_dir) and len(img_paths) > 0 and set_bg_imgs:
for img_path in img_paths:
img = cv2.imread(img_path)
self.bg_imgs.append(img)
first_bg = self.bg_imgs[0]
width = int(first_bg.shape[1] * bg_img_scale)
height = int(first_bg.shape[0] * bg_img_scale)
first_bg = cv2.resize(first_bg, (width, height), interpolation=cv2.INTER_AREA)
# 对齐图片,自动裁切中间
new_bg_imgs = [first_bg, ]
for img in self.bg_imgs[1:]:
width_close = abs(first_bg.shape[1] - img.shape[1]) < abs(first_bg.shape[0] - img.shape[0])
if width_close:
# resize
height = int(first_bg.shape[1] / img.shape[1] * img.shape[0])
width = first_bg.shape[1]
img = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)
# crop and fill
if img.shape[0] > first_bg.shape[0]:
crop_num = img.shape[0] - first_bg.shape[0]
crop_top = crop_num // 2
crop_bottom = crop_num - crop_top
img = np.delete(img, range(crop_top), axis=0)
img = np.delete(img, range(img.shape[0] - crop_bottom, img.shape[0]), axis=0)
elif img.shape[0] < first_bg.shape[0]:
fill_num = first_bg.shape[0] - img.shape[0]
fill_top = fill_num // 2
fill_bottom = fill_num - fill_top
img = np.concatenate([np.zeros([fill_top, width, 3]), img, np.zeros([fill_bottom, width, 3])], axis=0)
else:
width = int(first_bg.shape[0] / img.shape[0] * img.shape[1])
height = first_bg.shape[0]
img = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)
# crop and fill
if img.shape[1] > first_bg.shape[1]:
crop_num = img.shape[1] - first_bg.shape[1]
crop_top = crop_num // 2
crop_bottom = crop_num - crop_top
img = np.dele