最新 python 爱心代码?

本文介绍了Python中的一段爱心代码实现,同时讲解了如何在同一个文件夹下引用settings.yaml配置文件,为初学者提供了清晰的演示过程。
摘要由CSDN通过智能技术生成

 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值