计算文本大小

 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake(300, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    NSLog(@"rect = %@", NSStringFromCGRect(rect));
    
    label.frame = CGRectMake(0, 0, label.frame.size.width, rect.size.height);
    [label setText:str];
    [label setBackgroundColor:[UIColor yellowColor]];
    
    sc.contentSize = CGSizeMake(0, label.frame.size.height);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
from PIL import Image, ImageDraw, ImageFont import cv2 import numpy as np from threading import Thread def generate_video(text): # 视频分辨率 width = 90 height = 30 # 背景颜色和字体颜色 background_color = (255,0, 0, 255) # 红色背景 text_color = (255, 255, 255) # 白色字体 # 字体大小和类型 font_size = int(height * 0.35) font = ImageFont.truetype('msyh.ttc', font_size) # 计算文本大小 text_size = font.getsize(text) # 计算视频长度 text_length = len(text) * font_size video_length = int((width + text_length) / 20) # 创建视频写入器 fourcc = cv2.VideoWriter_fourcc(*'mp4v') video_writer = cv2.VideoWriter(r'E:\Template\word\marquee.mp4', fourcc, 60, (width, height)) def update_progress(progress): print('\r[{}{}] {:.2f}%'.format('#' * int(progress * 10), ' ' * (10 - int(progress * 10)), progress * 100), end='') def export_video(): for i in range(video_length): # 创建背景图像 img = Image.new('RGB', (width, height), background_color) # 添加文本 draw = ImageDraw.Draw(img) x = width - i * 10 y = int(height / 2 - text_size[1] / 2) draw.text((x, y), text, fill=text_color, font=font) # 写入视频帧 video_writer.write(cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)) # 更新进度条 update_progress(i / video_length) # 关闭视频写入器 video_writer.release() print('\n视频导出完成!') # 使用多线程优化视频导出速度 export_thread = Thread(target=export_video) export_thread.start() generate_video('sss') 优化代码根据文本的长度计算视频时长。 给我完整代码
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值