In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to achieve video character painting animation gadget based on Python, the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
Introduction
Let's bring you something fun today. I'm sure you'll like it!
Do you all know this one?
Yes, even character animation, have you seen it in that corner before, but you haven't tried it yourself yet.
The editor gives you a try first, so how to turn the video animation into character painting? Today I'm going to teach you how to convert. It's very simple. I'm going to teach you how to make it today.
This tool can be converted with one click!
Text 1. Preparation
1) if you have material, you can find a short video by yourself.
2) Environmental installation
The environment used in this paper is as follows: Python3, Pycharm, numpy, cv2 and some self-contained modules.
Module installation:
The second name and principle introduction of pip install-I https://pypi.douban.com/simple/ + module
1) the video can be converted into a picture frame by frame, which can be realized by using OpenCV.
2) * * convert each picture into a character painting. The principle is to determine what character to use for each pixel according to the pixel value of the picture.
3) just play the character painting in order.
3. Code demonstration # Video character-to-character animation import osimport cv2import sysimport timeimport ctypesimport subprocessimport numpy as np# dark blue FOREGROUND_DARKBLUE = 0x01# dark green FOREGROUND_DARKGREEN = 0x02# dark sky blue FOREGROUND_DARKSKYBLUE = 0x03# dark red FOREGROUND_DARKRED = 0x04# dark pink FOREGROUND_DARKPINK = 0x05# dark yellow FOREGROUND_DARKYELLOW = 0x06# dark white FOREGROUND_DARKWHITE = 0x07# dark gray FOREGROUND_DARKGRAY = 0x08# blue FOREGROUND_BLUE = 0x09# green FOREGROUND_ GREEN = 0x0a# sky blue FOREGROUND_SKYBLUE = 0x0b# red FOREGROUND_RED = 0x0c# pink FOREGROUND_PINK = 0x0d# yellow FOREGROUND_YELLOW = 0x0e# white FOREGROUND_WHITE = 0x0f# RGB value CMD _ colors = {'FOREGROUND_DARKBLUE': [FOREGROUND_DARKBLUE (0,0,139)], 'FOREGROUND_DARKGREEN': [FOREGROUND_DARKGREEN, (0,100,0)],' FOREGROUND_DARKSKYBLUE': [FOREGROUND_DARKSKYBLUE, (2,142,185)], 'FOREGROUND_DARKRED': [FOREGROUND_DARKRED, (139,0,0)] 'FOREGROUND_DARKPINK': [FOREGROUND_DARKPINK, (231,84,128)],' FOREGROUND_DARKYELLOW': [FOREGROUND_DARKYELLOW, (204,204,0)], 'FOREGROUND_DARKWHITE': [FOREGROUND_DARKWHITE, (255,250,250)] 'FOREGROUND_DARKGRAY': [FOREGROUND_DARKGRAY, (169,169,169)],' FOREGROUND_BLUE': [FOREGROUND_BLUE, (0,0,255)], 'FOREGROUND_GREEN': [FOREGROUND_GREEN, (0,128,0)] 'FOREGROUND_SKYBLUE': [FOREGROUND_SKYBLUE, (135,206,235)],' FOREGROUND_RED': [FOREGROUND_RED, (255,0,0)], 'FOREGROUND_PINK': [FOREGROUND_PINK, (255,192,203)] 'FOREGROUND_YELLOW': [FOREGROUND_YELLOW, (255,255,0)],' FOREGROUND_WHITE': [FOREGROUND_WHITE, (255,255,255)]} CHARS = ". -'`:! 1+*abcdefghijklmnopqrstuvwxyz ()\ / {} []? 234567890ABCDEFGHIJLMNOPQRSTUVWXYZ% video def video2imgs $"''Function: video to picture Input:-videopath: video path-size: specify picture size-interval: video takes one frame per interval frame Return:-img_list: image list' 'def video2imgs (videopath, size) Interval=1): img_list = list () capture = cv2.VideoCapture (videopath) I =-1 while capture.isOpened (): I + = 1 ret, frame = capture.read () if ret: if I% interval= = 0: # frame = cv2.cvtColor (frame Cv2.COLOR_BGR2GRAY) img = cv2.resize (frame, size Interpolation=cv2.INTER_AREA) img_list.append (img) else: break capture.release () return img_list'''# changes the RGB value of the picture to the color supported by the Windows-CMD window according to the distance def RGB2Cmdcolor (color): cmd_color = None min_distance = 1e6 for key Value in cmd_colors.items (): distance = np.square (np.array (color)-np.array (value [1])) .sum () if distance
< min_distance: min_distance = distance cmd_color = value[0] return cmd_color''''''Function: 图像转字符画Input: -img(np.array): 图像Return: -img_chars: 像素点对应的字符集合'''def img2chars(img): img_chars = [] height, width, channel = img.shape for row in range(height): line = "" for col in range(width): percent = int(np.array(img[row][col]).sum() / 3) / 255 char_idx = int(percent * (len(CHARS) - 1)) line += CHARS[char_idx] + ' ' img_chars.append(line) return img_chars'''Function: 视频转字符画sInput: -imgs: 视频里捕获的所有图片Return: -video_chars: img_chars的集合'''def imgs2chars(imgs): video_chars = [] for img in imgs: video_chars.append(img2chars(img)) return video_chars'''Function: 播放字符画sInput: -video_chars: imgs2chars中获取的video_chars -iscmd(bool): 是否在Windows的cmd窗口播放 -color: 颜色选择, cmd中有效'''def play(video_chars, color=None, iscmd=True): if color and iscmd: STD_OUTPUT_HANDLE = -11 std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) color_choice = None if color.isdigit(): color_choice = list(cmd_colors.values())[int(color)][0] else: color_choice = cmd_colors.get(color)[0] if color_choice is not None: _ = ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color_choice) width, height = len(video_chars[0][0]), len(video_chars[0]) for img_chars in video_chars: for row in range(height): print(img_chars[row]) time.sleep(1/24) if iscmd: os.system('cls') else: subprocess.call("clear")# 主函数def main(videopath, color=None, iscmd=True): imgs = video2imgs(videopath=videopath, size=(64, 48), interval=1) video_chars = imgs2chars(imgs) input("[INFO]: Complete Pre-processing! Enter button to start to play...") if iscmd: os.system('cls') else: subprocess.call("clear") play(video_chars, color=color, iscmd=True)if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('-f', '--file', help='Video file.') parser.add_argument('-c', '--color', help='Color for playing.') args = parser.parse_args() main(args.file, color=args.color)四、效果展示 1)截图效果展示 第一组随机截图:The above is based on Python how to achieve video character painting animation gadgets, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.