def main(): print("Hello") font = ImageFont.truetype(config['font'], config['title_font_size'], encoding="utf-8") font2 = ImageFont.truetype(config['font'], config['content_font_size'], encoding="utf-8") title_wrapper = text_processing.Wrapper(font) content_wrapper = text_processing.Wrapper(font2) generate_frame("test\opencv-gaussian-blur.png", "Title", "Content", (1280, 720), title_wrapper, content_wrapper, font, font2)
def create_frame(image_name, title, content, size, title_wrapper, content_wrapper, title_font, content_font): margin = config['margin'] picture_width = config['picture_width'] raw_content = content image = cv2.imread(image_name) height = int(image.shape[0] / image.shape[1] * picture_width) if (height > config['height'] - 2 * margin): height = config['height'] - 2 * margin picture_width = int(image.shape[1] / image.shape[0] * height) frame = Image.new('RGB', size, color=config['background_color']) # shrink image to fit in image area potential_height = config['height'] - margin * 2 ratio = max(image.shape[1] / picture_width, image.shape[0] / potential_height) content_image = cv2.resize( image, (int(image.shape[1] / ratio), int(image.shape[0] / ratio)), interpolation=cv2.INTER_CUBIC) actual_width = int(image.shape[1] / ratio) cv2img = cv2.cvtColor(content_image, cv2.COLOR_BGR2RGB) pilimg = Image.fromarray(cv2img) frame.paste(pilimg, (margin, margin)) draw = ImageDraw.Draw(frame) title = title_wrapper.wrap_string( title, config['width'] - picture_width - config['margin'] * 3) content = content_wrapper.wrap_string( content, config['width'] - picture_width - config['margin'] * 3) print(content) potential_height = config['height'] - margin * 2 y_offset = margin * 1.5 + title_font.getsize_multiline(title)[1] content_height = potential_height - y_offset + margin actual_content_height = content_font.getsize_multiline(content)[1] while (actual_content_height > content_height): content_font = shrink_font(content_font, config['content_font']) content_wrapper = text_processing.Wrapper(content_font) content = content_wrapper.wrap_string( raw_content, config['width'] - config['picture_width'] - config['margin'] * 3) print(content) actual_content_height = content_font.getsize_multiline(content)[1] print(actual_content_height) draw.text((picture_width + margin * 2, margin), title, config['title_color'], font=title_font) draw.text((picture_width + margin * 2, y_offset), content, config['content_color'], font=content_font) cv2charimg = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR) return cv2charimg
def generate_blank_frame(title, content, size, title_wrapper, content_wrapper, title_font, content_font): margin = config['margin'] picture_width = config['picture_width'] raw_content = content frame = Image.new('RGBA', size, color=config['background_color']) draw = ImageDraw.Draw(frame) title = title_wrapper.wrap_string( title, config['width'] - picture_width - config['margin'] * 3) content = content_wrapper.wrap_string( content, config['width'] - picture_width - config['margin'] * 3) potential_height = config['height'] - margin * 2 y_offset = margin * 1.5 + title_font.getsize_multiline(title)[1] content_height = potential_height - y_offset + margin actual_content_height = content_font.getsize_multiline(content)[1] while (actual_content_height > content_height): print("Too much information to display, Shrinking font size...") content_font = shrink_font(content_font, config['content_font']) content_wrapper = text_processing.Wrapper(content_font) content = content_wrapper.wrap_string( raw_content, config['width'] - content['picture_width'] - config['margin'] * 3) print(content) actual_content_height = content_font.getsize_multiline(content)[1] #print(actual_content_height) draw.text((picture_width + margin * 2, margin), title, config['title_color'], font=title_font) draw.text((picture_width + margin * 2, y_offset), content, config['content_color'], font=content_font) cv2charimg = np.array(frame) return cv2charimg
def create_blank_frame(title, content, size, title_wrapper, content_wrapper, title_font, content_font): margin = config['margin'] picture_width = config['picture_width'] raw_content = content frame = Image.new('RGB', size, color=config['background_color']) draw = ImageDraw.Draw(frame) title = title_wrapper.wrap_string( title, config['width'] - picture_width - config['margin'] * 3) content = content_wrapper.wrap_string( content, config['width'] - picture_width - config['margin'] * 3) potential_height = config['height'] - margin * 2 y_offset = margin * 1.5 + title_font.getsize_multiline(title)[1] content_height = potential_height - y_offset + margin actual_content_height = content_font.getsize_multiline(content)[1] while (actual_content_height > content_height): content_font = shrink_font(content_font, config['content_font']) content_wrapper = text_processing.Wrapper(content_font) content = content_wrapper.wrap_string( raw_content, config['width'] - config['picture_width'] - config['margin'] * 3) print(content) actual_content_height = content_font.getsize_multiline(content)[1] print(actual_content_height) draw.text((picture_width + margin * 2, margin), title, config['title_color'], font=title_font) draw.text((picture_width + margin * 2, y_offset), content, config['content_color'], font=content_font) cv2charimg = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR) return cv2charimg
def main(title: str, skip_crawling: bool): title = str(title) if (not skip_crawling): crawler.main(title) print("Start to create video for {}".format(title)) fps = config['fps'] width = config['width'] height = config['height'] # Paths output_dir = os.sep.join([".", "output"]) if not os.path.exists(output_dir): print("Folder", output_dir, 'does not exist. Creating...') os.makedirs(output_dir) resource_dir = os.sep.join([".", "resource", title]) # Assets result = text_processing.load_data(title) title_font = ImageFont.truetype(config['title_font'], config['title_font_size'], encoding="utf-8") content_font = ImageFont.truetype(config['content_font'], config['content_font_size'], encoding="utf-8") title_wrapper = text_processing.Wrapper(title_font) content_wrapper = text_processing.Wrapper(content_font) audio_clip = AudioFileClip( os.sep.join([resource_dir, "audio", title + ".mp3"])) # Video Properties fourcc = VideoWriter_fourcc(*'mp4v') video = VideoWriter(os.sep.join([output_dir, title + '_complex_temp.mp4']), fourcc, float(fps), (width, height)) # Create Video keys = list(map(int, result.keys())) if 0 not in keys: keys.append(0) frame = image_processing.generate_cv2_title_image( os.sep.join(['.', 'resource', title, 'title.jpg']), (width, height)) else: key = "0" image = os.sep.join( [resource_dir, str(key) + result[key]['image_suffix']]) header = result[key]['header'] content = result[key]['content'] print("标题:{}".format(header)) if (result[key]['image_suffix'] in ['.gif', '.GIF']): frame = image_processing.generate_cv2_blank_frame( header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) else: frame = image_processing.generate_cv2_frame( image, header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) #os.remove(image) keys.sort() # Set last picture to be 20 seconds long keys.append(math.ceil(audio_clip.duration)) #print(keys) # Number of frames in this video total_length = (200 if config['test'] else keys[len(keys) - 1]) * fps index = 0 for i in range(total_length): if (index > len(keys) - 1): frame = image_processing.generate_cv2_blank_frame( "", "", (width, height), title_wrapper, content_wrapper, title_font, content_font) elif (i / fps) >= keys[index + 1]: index += 1 print("Processing {} frames out of {}".format( index, len(keys) - 1)) key = str(keys[index]) image = os.sep.join( [resource_dir, str(key) + result[key]['image_suffix']]) header = result[key]['header'] content = result[key]['content'] print("标题:{}".format(header)) if (result[key]['image_suffix'] in ['.gif', '.GIF']): frame = image_processing.generate_cv2_blank_frame( header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) else: frame = image_processing.generate_cv2_frame( image, header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) #os.remove(image) else: pass video.write(frame) video.release() video_clip = VideoFileClip( os.sep.join([output_dir, title + "_complex_temp.mp4"])) print(video_clip.duration) video_clip.audio = audio_clip if config['enable_logo']: logo_clip = video_processing.load_logo(os.sep.join( [".", "util", config['logo_name']]), duration=video_clip.duration) video_clip = video_processing.add_logo(video_clip, logo_clip) if config['test']: video_clip = video_clip.subclip(0, min(200, video_clip.duration)) video_clip.write_videofile(os.sep.join( [output_dir, title + "_complex.mp4"]), fps=fps) print("{} finished!".format(title)) os.remove(os.sep.join([output_dir, title + "_complex_temp.mp4"]))
def generate_frame(image_url, title, content, size, title_wrapper, content_wrapper, title_font, content_font): margin = config["margin"] picture_width = config['picture_width'] raw_content = content # Read image from file try: image = cv2.imread(image_url) except: return generate_blank_frame(title, content, size, title_wrapper, content_wrapper, title_font, content_font) # expand image for background ratio = max(size[0] / image.shape[1], size[1] / image.shape[0]) + 0.01 background_image = cv2.resize( image, (int(image.shape[1] * ratio), int(image.shape[0] * ratio)), interpolation=cv2.INTER_CUBIC) background_image = cv2.GaussianBlur(background_image, (255, 255), 255) left = int((background_image.shape[1] - size[0]) / 2) right = left + size[0] top = int((background_image.shape[0] - size[1]) / 2) bottom = top + size[1] # shrink image to fit in image area potential_height = config['height'] - margin * 2 ratio = max(image.shape[1] / picture_width, image.shape[0] / potential_height) content_image = cv2.resize( image, (int(image.shape[1] / ratio), int(image.shape[0] / ratio)), interpolation=cv2.INTER_CUBIC) actual_width = int(image.shape[1] / ratio) # cut background image to fit frame size background_image = background_image[top:bottom, left:right] background_cv2img = cv2.cvtColor(background_image, cv2.COLOR_BGR2RGB) content_cv2img = cv2.cvtColor(content_image, cv2.COLOR_BGR2RGB) frame = Image.fromarray(background_cv2img).convert('RGBA') content_frame = Image.fromarray(content_cv2img) frame.paste(content_frame, (margin, margin)) # add background for text area board = Image.new( 'RGBA', (size[0] - actual_width - int(margin * 1.5), config['height']), color=(255, 255, 255, 120)) frame.paste(board, (actual_width + int(margin * 1.5), 0), mask=board) draw = ImageDraw.Draw(frame) title = title_wrapper.wrap_string( title, config['width'] - actual_width - config['margin'] * 3) content = content_wrapper.wrap_string( content, config['width'] - actual_width - config['margin'] * 3) print(content) y_offset = margin * 1.5 + title_font.getsize_multiline(title)[1] content_height = potential_height - y_offset + margin actual_content_height = content_font.getsize_multiline(content)[1] while (actual_content_height > content_height): content_font = shrink_font(content_font, config['content_font']) content_wrapper = text_processing.Wrapper(content_font) content = content_wrapper.wrap_string( raw_content, config['width'] - actual_width - config['margin'] * 3) print(content) actual_content_height = content_font.getsize_multiline(content)[1] print(actual_content_height) print("Out") draw.text((actual_width + margin * 2, margin), title, config['title_color'], font=title_font) draw.text((actual_width + margin * 2, y_offset), content, config['content_color'], font=content_font) cv2charimg = np.array(frame) #cv2.imshow("Image", cv2charimg) #cv2.waitKey(0) #print("Hello") return cv2charimg
def main(title:str, skip_crawling:bool): title=str(title) if(not skip_crawling): crawler.main(title) print("Start to create video for {}".format(title)) fps = config['fps'] width = config['width'] height = config['height'] # Paths output_dir = os.sep.join([".", "output"]) if not os.path.exists(output_dir): print("Folder", output_dir, 'does not exist. Creating...') os.makedirs(output_dir) resource_dir = os.sep.join([".", "resource", title]) # Assets result = text_processing.load_data(title) title_font = ImageFont.truetype(config['title_font'], config['title_font_size'], encoding="utf-8") content_font = ImageFont.truetype(config['content_font'], config['content_font_size'], encoding="utf-8") title_wrapper = text_processing.Wrapper(title_font) content_wrapper = text_processing.Wrapper(content_font) # Video Properties fourcc = VideoWriter_fourcc(*'mp4v') video = VideoWriter(os.sep.join([output_dir, title+'_simple.mp4']), fourcc, float(fps), (width, height)) # Create Video keys = list(map(int, result.keys())) if 0 not in keys: keys.append(0) frame = image_processing.create_blank_frame("", "", (width, height), title_wrapper, content_wrapper, title_font, content_font) else: key = "0" image = os.sep.join([resource_dir, str(key)+result[key]['image_suffix']]) header = result[key]['header'] content = result[key]['content'] print("标题:{}".format(header)) if(result[key]['image_suffix'] in ['.gif', '.GIF']): frame = image_processing.create_blank_frame(header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) else: frame = image_processing.create_frame(image, header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) #os.remove(image) keys.sort() # Set last picture to be 20 seconds long keys.append(keys[len(keys)-1]+20) #print(keys) # Number of frames in this video total_length = keys[len(keys)-1]*fps index = 0 for i in range(total_length): if(index+1>len(keys)-1): frame = image_processing.create_blank_frame("","", (width, height), title_wrapper, content_wrapper, title_font, content_font) elif (i/fps)>keys[index+1]: index+=1 print("Processing {} frames out of {}".format(index, len(keys)-1)) key = str(keys[index]) image = os.sep.join([resource_dir, str(key)+result[key]['image_suffix']]) header = result[key]['header'] content = result[key]['content'] print("标题:{}".format(header)) if(result[key]['image_suffix'] in ['.gif', '.GIF']): frame = image_processing.create_blank_frame(header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) else: frame = image_processing.create_frame(image, header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) #os.remove(image) else: pass video.write(frame) print("{} finished!".format(title))
def main(title: str): title = str(title) fps = config['fps'] result, audio_url = crawler.crawler(title) width = config['width'] height = config['height'] for key in result.keys(): image_name = str(key) image_url = result[key]['image_url'] image_dir = os.sep.join([".", "resource", title]) crawler.save_image(image_url, image_dir, image_name) fourcc = VideoWriter_fourcc(*'mp4v') output_dir = os.sep.join(['.', 'output']) if not os.path.exists(output_dir): print("Folder", output_dir, 'does not exist. Creating...') os.makedirs(output_dir) video = VideoWriter(os.sep.join([output_dir, str(title) + '.mp4']), fourcc, float(config['fps']), (config['width'], config['height'])) font = ImageFont.truetype(config['font'], config['title_font_size'], encoding="utf-8") font2 = ImageFont.truetype(config['font'], config['content_font_size'], encoding="utf-8") title_wrapper = text_processing.Wrapper(font) content_wrapper = text_processing.Wrapper(font2) keys = list(result.keys()) keys.append(0) keys.sort() keys.append(keys[len(keys) - 1] + 10) print(keys) frame = image_processing.create_blank_frame("", "", (width, height), title_wrapper, content_wrapper, font, font2) total_length = keys[len(keys) - 1] * fps index = 0 for i in range(total_length): if (index + 1 > len(keys) - 1): frame = image_processing.create_blank_frame( "", "", (width, height), title_wrapper, content_wrapper, font, font2) elif (i / fps) > keys[index + 1]: index += 1 print(index, "out of", len(keys)) key = keys[index] image = image = os.sep.join([ '.', 'resource', title, str(key) + text_processing.find_image_suffix(result[key]['image_url']) ]) header = result[key]['header'] content = result[key]['content'] print("标题:", header) if (result[key]['image_suffix'] in ['.gif', '.GIF']): frame = image_processing.create_blank_frame( header, content, (width, height), title_wrapper, content_wrapper, font, font2) else: frame = image_processing.create_frame(image, header, content, (width, height), title_wrapper, content_wrapper, font, font2) os.remove(image) else: "" video.write(frame) print(title, "finished!")
def main(title: str, skip_crawling: bool): title = str(title) if (not skip_crawling): crawler.main(title) print("Start to create video for {}".format(title)) fps = config['animation_fps'] width = config['width'] height = config['height'] test = config['test'] # Paths output_dir = os.sep.join([".", "output"]) if not os.path.exists(output_dir): print("Folder", output_dir, 'does not exist. Creating...') os.makedirs(output_dir) resource_dir = os.sep.join([".", "resource", title]) # Assets result = text_processing.load_data(title) title_font = ImageFont.truetype(config['title_font'], config['title_font_size'], encoding="utf-8") content_font = ImageFont.truetype(config['content_font'], config['content_font_size'], encoding="utf-8") title_wrapper = text_processing.Wrapper(title_font) content_wrapper = text_processing.Wrapper(content_font) audio_clip = AudioFileClip( os.sep.join([".", "resource", title, "audio", title + ".mp3"])) if not os.path.exists(output_dir): print("Folder", output_dir, 'does not exist. Creating...') os.makedirs(output_dir) keys = list(map(int, result.keys())) if 0 not in keys: keys.append(0) keys.append(math.ceil(audio_clip.duration)) keys.sort() #print(keys) video_clips = [] key_length = 10 if test else len(keys) - 1 files = os.listdir(os.sep.join(['.', 'resource', title])) print(files) for i in range(0, key_length): key = str(keys[i]) start = keys[i] end = keys[i + 1] #image_dir = os.sep.join(['.', 'resource', key+result[key]['image_suffix']]) if ((key not in result.keys()) or (key + result[key]['image_suffix'] not in files)): print("Case1") if key == '0': print("Creating title...") frame = image_processing.generate_title_image( os.sep.join(['.', 'resource', title, 'title.jpg']), (width, height)) else: frame = image_processing.generate_blank_frame( "", "", (width, height), title_wrapper, content_wrapper, title_font, content_font) videoclip = video_processing.create_video_with_frame( frame, start, end) video_clips.append(videoclip) else: if (result[key]['image_suffix'].lower() not in [".gif"]): print("Case2") image = os.sep.join([ '.', 'resource', title, str(key) + result[key]['image_suffix'] ]) header = result[key]['header'] content = result[key]['content'] frame = image_processing.generate_frame( image, header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) videoclip = video_processing.create_video_with_frame( frame, start, end) video_clips.append(videoclip) #os.remove(image) elif (result[key]['image_suffix'].lower() in [".gif"]): print("Case3") image = os.sep.join([ '.', 'resource', title, str(key) + result[key]['image_suffix'] ]) print(image) header = result[key]['header'] content = result[key]['content'] if config['skip_gif']: background_frame = image_processing.generate_blank_frame( header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) videoclip = video_processing.create_video_with_frame( background_frame, start, end) else: gif_clip = video_processing.load_gif_clip(image) background_frame = image_processing.generate_blank_frame( header, content, (width, height), title_wrapper, content_wrapper, title_font, content_font) videoclip = video_processing.create_video_with_gif_clip( background_frame, gif_clip, start, end) video_clips.append(videoclip) merged_clips = concatenate_videoclips(video_clips) merged_clips.audio = audio_clip logo_clip = video_processing.load_logo(os.sep.join( [".", "util", config['logo_name']]), duration=merged_clips.duration) if config['enable_logo']: final_clip = video_processing.add_logo(merged_clips, logo_clip) else: final_clip = merged_clips if test: final_clip = video_processing.add_logo( merged_clips, logo_clip).subclip(0, min(50, final_clip.duration)) final_clip.write_videofile(os.sep.join( [".", "output", title + "_animated.mp4"]), fps=fps, threads=4) print(title, "finished!")