import cv2
import numpy as np
# f0,f1='feiyong.jpg','g3logo.jpg'
# # #f1,f0='feiyong.jpg','g3logo.jpg'
# # img0,img1=cv2.imread(f0,),cv2.imread(f1,)
# # h,w,_=img0.shape
# # h_roi,w_roi=int(h/),int(w/)
# # img0_roi,img1_roi=img0[:h_roi,:w_roi],img1[:h_roi,:w_roi]
# # dst=np.zeros((h_roi,w_roi,),np.uint8)
# # dst=cv2.addWeighted(img0_roi,0.5,img1_roi,0.5,)
# # cv2.imshow('mydeal',dst)
# # cv2.waitKey() # -*- coding: utf- -*-
import cv2
import numpy as np f0,f1='feiyong.jpg','g3logo.jpg' img1 = cv2.imread(f0) # 加载图像
img2 = cv2.imread(f1) rows,cols,channels = img2.shape
roi = img1[:rows, :cols ] # 取img1的这个区域来处理
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) # 建立logo的二值图,也建立相反的二值图
ret, mask = cv2.threshold(img2gray, , , cv2.THRESH_BINARY)#二值化
mask_inv = cv2.bitwise_not(mask) # 做非操作,黑的变白,白的变黑,黑色0,白色255
img1_bg = cv2.bitwise_and(roi,roi,mask = mask) #与操作 ,参数输入,输出,与mask做and操作,黑色的被填充
img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv) #与操作
dst = cv2.add(img1_bg,img2_fg) # 相加
img1[:rows, :cols ] = dst #把添加了logo的该区域赋值回原来的地方 cv2.imshow('my--------------',img1) cv2.imshow('img2gray',img2gray) #显示整个过程中的中间结果和最终结果
cv2.imshow('ret',ret)
cv2.imshow('mask',mask)
cv2.imshow('mask_inv',mask_inv)
cv2.imshow('img1_bg',img1_bg)
cv2.imshow('img2_fg',img2_fg)
cv2.imshow('dst',dst)
cv2.imshow('res',img1)
cv2.waitKey()
cv2.destroyAllWindows()

在原视频的生成基础上,可以在不同字幕、不同播放位置贴入不同图片;

# 设置分句的标志符号;可以根据实际需要进行修改
# cutlist = "。!?".decode('utf-8') cutlist = ['\n', '\t', '。', ';', '?', '.', ';', '?', '...', '、、、', ':']
cutlist = ['\n', '\t', '。', ';', '?', ':'] # cutlist = [ '。', ';', '?', '.', ';', '?', '...', '、、、',':',':',',']
# cutlist = [ '。', ';', '?', '.', ';', '?', '...', '、、、',':',',','、'] # 检查某字符是否分句标志符号的函数;如果是,返回True,否则返回False
def FindToken(cutlist, char):
if char in cutlist:
return True
else:
return False # 进行分句的核心函数
def Cut(cutlist, lines): # 参数1:引用分句标志符;参数2:被分句的文本,为一行中文字符
l = [] # 句子列表,用于存储单个分句成功后的整句内容,为函数的返回值
line = [] # 临时列表,用于存储捕获到分句标志符之前的每个字符,一旦发现分句符号后,就会将其内容全部赋给l,然后就会被清空 for i in lines: # 对函数参数2中的每一字符逐个进行检查 (本函数中,如果将if和else对换一下位置,会更好懂)
if FindToken(cutlist, i): # 如果当前字符是分句符号
line.append(i) # 将此字符放入临时列表中
l.append(''.join(line)) # 并把当前临时列表的内容加入到句子列表中
line = [] # 将符号列表清空,以便下次分句使用
else: # 如果当前字符不是分句符号,则将该字符直接放入临时列表中
line.append(i)
return l r_s = []
# 以下为调用上述函数实现从文本文件中读取内容并进行分句。
# with open('mybaidu.parp.b.txt','r',encoding='utf-8') as fr :
# for lines in fr:
# l = Cut(list(cutlist), list(lines))
# for line in l:
# if len(line.replace(' ', '')) == :
# continue
# if line.strip() != "":
# line=line.strip()
# r_s.append(line)
#
# # li = line.strip().split()
# # for sentence in li:
# # r_s.append(sentence)
str_ = '' # cutlist = [ '。', ';', '?', '.', ';', '?', '...', '、、、',':',':',',','\n'] with open('mybaidu.parp.b.txt', 'r', encoding='utf-8') as fr:
for lines in fr:
if len(lines.replace(' ', '')) == :
continue
# str_='{}{}'.format(str_,lines.replace('\n',''))
# if len(lines.replace(' ','').replace('\n',''))==:
# continue
str_ = '{}{}'.format(str_, lines)
# l = Cut(list(cutlist), list(lines))
# for line in l:
# if line.strip() != "":
# line=line.strip() from aip import AipSpeech bd_k_l = ['', '5Kk01GtG2fjCwpzEkwdn0mjw', 'bp6Wyx377Elq7RsCQZzTBgGUFzLm8G2A']
APP_ID, API_KEY, SECRET_KEY = bd_k_l mp3_dir = 'C:\\Users\\sas\\PycharmProjects\\produce_video\\result_g3com\\'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# result = client.synthesis(str_, 'zh', , {
# 'vol': ,
# })
uid = 'liukeyuanG3_whole_para'
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
f_w = '{}{}{}{}{}'.format(mp3_dir, 'g3db', uid, 'g3uid', '.mp3') #
# if not isinstance(result, dict):
# # f_w = '{}{}{}{}'.format(mp3_dir, 'g3uid', uid, '.mp3')
# f_w = '{}{}{}{}{}'.format(mp3_dir, 'g3db', uid, 'g3uid', '.mp3')
# # ,'g3db',uid,'g3uid'
# # with open('auido.b.mp3', 'wb') as f:
# with open(f_w, 'wb') as f:
# f.write(result) sentence_l, sentence_l_chk = Cut(list(cutlist), list(str_)), []
for i in sentence_l:
chk_br = i.replace('\n', ' ')
# del sentence_l[sentence_l.index(i)]
if len(chk_br.replace(' ', '')) > :
sentence_l_chk.append(chk_br.replace(' ', '')) bdmp3filter_l = [':', '——', ',', '《', '》', '“', '”', '、', '(', ')', '.', ' '] # 注意空格 # 保留其他标点符号
mp3_str = ' '.join(sentence_l_chk)
mp3_str_bdmp3filter = mp3_str
for i in bdmp3filter_l:
mp3_str_bdmp3filter = mp3_str_bdmp3filter.replace(i, '') import os, time, glob
import cv2 os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[
-] logo_f_name, logo_f = 'g3logo.jpg', '' f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.jpg')
imgs, img_size_d = glob.glob(f_img_d), {} for i in imgs:
if logo_f_name in i:
logo_f = i
del imgs[imgs.index(i)] for i in imgs:
if logo_f_name in i:
logo_f = i
img = cv2.imread(i)
w_h_s = '{},{}'.format(img.shape[], img.shape[])
if w_h_s not in img_size_d:
img_size_d[w_h_s] =
else:
img_size_d[w_h_s] += mode_img_size_wh = [int(i) for i in
sorted(img_size_d.items(), key=lambda mytuple: mytuple[], reverse=True)[][].split(',')]
mode_img_size_wh = [, ]
mode_img_size_wh = [, ]
os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[-] import time, math this_time = time.time()
import imageio imageio.plugins.ffmpeg.download()
from moviepy.editor import VideoFileClip f_mp3 = 'g3dbG3g3uidnoBRBlankLine.06.mp3'
import mutagen.id3
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3 EasyID3.valid_keys["comment"] = "COMM::'XXX'"
id3info = MP3(f_mp3, ID3=EasyID3)
t_spend = id3info.info.length import cv2
import glob '''
python+opencv视频图像相互转换 - CSDN博客 https://blog.csdn.net/m0_37733057/article/details/79023693
链接:https://www.zhihu.com/question/49558804/answer/343058915 OpenCV: Drawing Functions in OpenCV https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html '''
# 每秒传输帧数(Frames Per Second)
fps = # 保存视频的FPS,可以适当调整 FPS是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。FPS是测量用于保存、显示动态视频的信息数量。每秒钟帧数愈多,所显示的动作就会愈流畅。通常,要避免动作不流畅的最低是30。某些计算机视频格式,每秒只能提供15帧。 f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.jpg')
imgs = glob.glob(f_img_d) """
用图片总数均分音频时间
""" def resize_rescale_pilimg(img_f, w_h_tuple=(mode_img_size_wh[], mode_img_size_wh[]), mid_factor=):
# print(img_f)
img_n, img_type = img_f.split('.')[-], img_f.split('.')[-]
# print(img_n) img_n_resize_rescale_pilimg_dir = '{}{}{}'.format(os_sep.join(img_n.split(os_sep)[:-]), 'resize_rescale_pilimg',
os_sep, img_n.split(os_sep)[-], os_sep) img_n_resize_rescale_pilimg = '{}{}{}'.format(img_n_resize_rescale_pilimg_dir, img_n.split(os_sep)[-], '.PNG')
# print(img_n_resize_rescale_pilimg) img_type = 'PNG'
# img_f_new = '{}{}{}{}'.format(img_n, int(time.time()), 'resize_rescale.', img_type)
img_f_new = img_n_resize_rescale_pilimg
mid_icon = Image.open(img_f)
mid_icon_w, mid_icon_h = w_h_tuple[] * mid_factor, w_h_tuple[] * mid_factor
mid_icon = mid_icon.resize((mid_icon_w, mid_icon_h), Image.ANTIALIAS)
mid_icon.save(img_n_resize_rescale_pilimg, img_type)
return img_f_new from PIL import Image, ImageDraw, ImageFont # myfont = ImageFont.truetype("simhei.ttf", , encoding="utf-8")
myfont = ImageFont.truetype("simhei.ttf", encoding="utf-8") import cv2
import numpy as np equal_str_l = []
le = len(mp3_str)
br_step =
br_step = math.floor((mode_img_size_wh[]) * 0.038)
br_step = math.floor((mode_img_size_wh[]) * 0.036)
br_times = math.ceil(le / br_step)
for i_br_loop in range(br_times):
s_p = mp3_str[i_br_loop * br_step:i_br_loop * br_step + br_step]
equal_str_l.append(s_p)
l = equal_str_l
char_loop_l_len = len(mp3_str) / len(equal_str_l)
char_loop_l_len = len(mp3_str_bdmp3filter) '''
bdmp3filter_l = [':', '——', ',', '《', '》', '“', '”', '、', '(', ')', '.', ' '] # 注意空格 # 保留其他标点符号
mp3_str = ' '.join(sentence_l_chk)
mp3_str_bdmp3filter = mp3_str
for i in bdmp3filter_l:
mp3_str_bdmp3filter = mp3_str_bdmp3filter.replace(i, '') ''' import numpy as np def gen_video(os_delay_factor=0.245, mystep=0.01, bear_error_second=, audio_spend=t_spend, step_para=):
f_v = '{}{}{}{}{}{}{}'.format('D:\\myv\\', 'g3db', uid, 'g3uid', uid, int(time.time()), 'saveVideo.b.avi')
fps, fourcc = , cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
videoWriter = cv2.VideoWriter(f_v, fourcc, fps, (mode_img_size_wh[], mode_img_size_wh[]))
for i in l:
# print(i)
i_index = l.index(i)
img_index = i_index % len(imgs)
imgname = imgs[img_index]
mystr, le = i, len(i)
frame = cv2.imread(imgname)
if (frame.shape[], frame.shape[]) != (mode_img_size_wh[], mode_img_size_wh[]):
imgname = resize_rescale_pilimg(imgname)
frame = cv2.imread(imgname)
else:
pass img1 = cv2.imread(imgname) # 加载图像
img2 = cv2.imread(logo_f) #logo
rows, cols, channels = img2.shape
roi = img1[:rows, :cols] # 取img1的这个区域来处理
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 建立logo的二值图,也建立相反的二值图
ret, mask = cv2.threshold(img2gray, , , cv2.THRESH_BINARY) # 二值化
mask_inv = cv2.bitwise_not(mask) # 做非操作,黑的变白,白的变黑,黑色0,白色255
img1_bg = cv2.bitwise_and(roi, roi, mask=mask) # 与操作 ,参数输入,输出,与mask做and操作,黑色的被填充
img2_fg = cv2.bitwise_and(img2, img2, mask=mask_inv) # 与操作
dst = cv2.add(img1_bg, img2_fg) # 相加
img1[:rows, :cols] = dst # 把添加了logo的该区域赋值回原来的地方 frame =img1 frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_pil = Image.fromarray(frame_cv2) # 转为PIL的图片格式
# font = ImageFont.truetype("simhei.ttf", , encoding="utf-8")
font_size = math.floor((mode_img_size_wh[]) * 0.020)
font = ImageFont.truetype("simhei.ttf", font_size, encoding="utf-8")
# ImageDraw.Draw(frame_pil).text((, ), mystr, (, , ), font)
f_x, f_y = math.floor((mode_img_size_wh[]) * 0.06), math.floor(mode_img_size_wh[] * 0.85)
# ImageDraw.Draw(frame_pil).text((, mode_img_size_wh[]-), mystr, (, , ), font)
ImageDraw.Draw(frame_pil).text((f_x, f_y), mystr, (, , ), font)
frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
img = frame_cv2 line_bdmp3filter = i
for bdmp3filter in bdmp3filter_l:
line_bdmp3filter = line_bdmp3filter.replace(bdmp3filter, '')
myinterval = t_spend / (len(mp3_str_bdmp3filter) * ) * os_delay_factor * len(line_bdmp3filter)
print(myinterval, '---------------', mystr)
this_time = time.time()
while time.time() - this_time < myinterval:
videoWriter.write(img)
videoWriter.release()
time.sleep()
print(f_v)
video_playtime = VideoFileClip(f_v).duration
if video_playtime - audio_spend > bear_error_second:
# os_delay_factor -= mystep
os_delay_factor *= t_spend / video_playtime
gen_video(os_delay_factor=os_delay_factor, mystep=0.005, audio_spend=t_spend)
elif audio_spend - video_playtime > bear_error_second:
# os_delay_factor += mystep
os_delay_factor *= t_spend / video_playtime
gen_video(os_delay_factor=os_delay_factor, mystep=0.005, audio_spend=t_spend)
else:
os._exit() ''' '''
gen_video(os_delay_factor=0.046, mystep=0.03, bear_error_second=0.5, audio_spend=t_spend)

加logo的更多相关文章

  1. 给网页头部标题加logo

    现在在写公司的官网,需要在网页的头部加logo,没有加的时候是这样的 那么,现在只要一步,就可以了,加上一行代码 <link rel="icon" href="图标 ...

  2. asp.net生成店铺推广二维码,二维码中间加logo(源码)

    二维条码比一维条码记载数据量更多,二维码条码是一种高密度.高信息含量的便携式数据文件,是实现证件及卡片等大容量.高可靠性信息自动存储.携带并可用机器自动识读的理想手段.而且可以记载更复杂的数据,比如图 ...

  3. C# 生成二维码并且在中间加Logo

    今天做项目的时候有个在生成二维码并且在中间加入Logo的需求,动手试了几把,总感觉效果没有之前写的好,就翻出旧代码,果然还是熟悉的味道,生成一张效果图如下 左边是微信里面的,右边是我自己生成的 原理比 ...

  4. ffmpeg 加 logo

    How to add a watermark or logo to any corner or the center of a video with FFMPEG. ffmpeg –i video.m ...

  5. asp.net(C#)利用QRCode生成二维码(续)-在二维码图片中心加Logo或图像

    <%@ WebHandler Language="C#" Class="GetQRCode" %> using System; using Syst ...

  6. 网页Title加LOGO图标

    <link rel="icon" href="XXX.ico" type="image/x-icon"/> 只能放格式为.ico ...

  7. 【转】C# 生成二维码并且在中间加Logo(图片合并)

    public class QRCodeHelper { public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidt ...

  8. [Java] 生成二维码源码,可以在二维码中间加logo,底部可以加文字介绍

    链接:https://pan.baidu.com/s/1bc1h-ix-No-2o9Ysd4_B3Q提取码:0ad4

  9. 图片加水印文字,logo。生成缩略图

    简单JSP代码 图片加水银文字 try { String path = request.getRealPath("images\\01.jpg"); out.print(path) ...

随机推荐

  1. Tcl之Math

    expr is for Tcl to do math operations. It takes all of its arguments ("2 + 2" for example) ...

  2. 关于联想笔记本不能连接无线网(wifi),注销后重新登录才可以连接

    解决联想笔记本wifi问题(果果) 最近很多使用联想的朋友都遇到了这样一个问题,那就是笔记本的wifi突然不能用了,好吧,其实我个人也遇到了这个问题,但是网上貌似对这个问题并没有给出一个可以解决的办法 ...

  3. AMH V4.5 – 基于AMH4.2的第三方开发版

    AMH V4.5[基于AMH4.2第三方开发版]重新部署了一次安装脚本,修改一系列BUG,已完美支持CENTOS7,树莓派,Fedora,Aliyun,Amazon,debian,Ubuntu,Ras ...

  4. golang zip 压缩,解压(含目录文件)

    每天学习一点go src. 今天学习了zip包的简单使用,实现了含目录的压缩与解压. 写了两个方法,实现了压缩.解压. package ziptest import ( "archive/z ...

  5. jQuery.treetable使用及异步加载

    Usage 1 GitHub 地址 https://github.com/ludo/jquery-treetable/ 2 API 地址 http://ludo.cubicphuse.nl/jquer ...

  6. MySQL学习笔记(十二)__连接查询(一)

    连接查询含义:又称多表查询,当查询的字段来自多个表时,就会用到连接查询 笛卡尔乘积现象:表1 有 m 行,表2 有 n 行,结果 = m*n 行发生原因:没有有效的连接条件如何避免:添加有效的连接条件 ...

  7. ios xmpp 发送语音图片解决方案

    ios xmpp 发送语音,图片解决方案,有需要的朋友可以参考下. 目前做IM多是用的xmpp. 因为项目需求需要实现语音和图片的发送. 发送语音图片有三种方法. 1,xmpp smack.文件传输方 ...

  8. zabbix+docker

    由于公司线上服务器数量太少,又要用于生产,领导让上zabbix,但熟知zabbix搭建需要LAMP或者LNMP,如果和生产服务器混搭的话,不方便管理,也怕出问题,所以就先使用docker方式搭建管理. ...

  9. Codeforces 938C - Constructing Tests

    传送门:http://codeforces.com/contest/938/problem/C 给定两个正整数n,m(m≤n),对于一个n阶0-1方阵,其任意m阶子方阵中至少有一个元素“0”,则可以求 ...

  10. node.js 核心http模块,起一个服务器,返回一个页面

    let http=require("http"); //引入核心http模块 let fs=require("fs"); let mime={ '.js':'a ...