In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Python code to make dynamic firecrackers. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The effect is as follows:
The basic principle of dynamic firecrackers is to reproduce a recorded firecracker video in the form of character painting. The basic step is to convert frame sampling → into character painting → character painting composite video frame by frame. Let's get started!
1 Video frame sampling
The function is shown below, and the main function is to save the video image stream to a specific cache folder frame by frame (automatically created if the folder does not exist). The input vp of the function is the openCV video handle, and the output number is the number of converted pictures.
Def video2Pic (vp): number = 0 if vp.isOpened (): r if vp.isOpened frame = vp.read () if not os.path.exists ('cachePic'): os.mkdir (' cachePic') os.chdir ('cachePic') else: r = False while r: number + = 1 cv2.imwrite (str (number) +' .jpg', frame) r Frame = vp.read () os.chdir ("..") Return number2 converts the picture to character drawing 2.1 to create pixel-character index
The function inputs the pixel RGBA value and outputs the corresponding character code. The principle is that the characters are evenly distributed in the whole grayscale range, and the pixel grayscale value falls in which interval corresponds to the character code. Character codes can be referred to ASCII codes.
ASCII codes use a specified combination of 7-bit or 8-bit binary numbers to represent 128 or 256 possible characters. Standard ASCII codes, also known as base ASCII codes, use 7-digit binary numbers (the remaining 1-bit binary is 0) to represent all uppercase and lowercase letters, the numbers 0 to 9, punctuation, and special control characters used in American English. Among them, 31,127 (33 in total) are control characters or communication special characters (the rest are displayable characters), such as control characters: LF (line feed), CR (carriage return), FF (page feed), DEL (delete), BS (backspace), BEL (ring ring), etc.; communication special characters: SOH (header), EOT (tail), ACK (confirmation), etc. ASCII values of 8, 9, 10, and 13 are converted to backspace, tabulation, line feed, and carriage return characters, respectively. They do not have a specific graphical display, but they have different effects on text display depending on the application.
RGBA is the color space that represents Red (red), Green (green), Blue (blue), and Alpha, and the Alpha channel is generally used as an opacity parameter. If a pixel's alpha channel value is 0%, it is completely transparent, while a value of 100% means a completely opaque pixel (a traditional digital image). Gray=0.2126 * r + 0.7152 * g + 0.0722 * b is the empirical formula from RGB to gray value, and human eyes are more sensitive to green.
Def color2Char: imgChar= list ("# RMNHQODBWGPZ*@$C&98?32I1 >!: -.") If alpha: gray = int (0.2126 * r + 0.7152 * g + 0.0722 * b) unit = 0.2126 / len (imgChar) return imgChar [int (gray / unit)] else: return''2.2 convert pictures to characters pixel by pixel
The core code is as follows, traversing each pixel of the picture
Img = Image.open (imagePath) .convert ('RGB'). Resize ((imgWidth, imgHeight), Image.NEAREST) for i in range (imgHeight): for j in range (imgWidth): pixel = img.getpixel ((j, I) color.append ((pixel [0], pixel [1], pixel [2]) txt = txt + color2Char (pixel [0], pixel [1], pixel [2]) Pixel [3]) if len (pixel) = = 4 else\ txt + color2Char (pixel [0], pixel [1], pixel [2]) txt + ='\ n' color.append ((255255255)) 3 synthesize character images into video
The input parameter vp is the openCV video handle, number is the number of frames, and savePath is the video preservation path. MP42 in the function is an encoding method that can generate smaller and smaller video files. Other similar parameters include isom, mp41, avc1, qt and so on, indicating which format is best to parse the current file.
Def img2Video (vp, number, savePath): videoFourcc = VideoWriter_fourcc (* "MP42") # set video encoder asciiImgPathList = ['cacheChar' + rpm / {} .jpg' .format (I) for i in range (1, number + 1)] asciiImgTemp = Image.open (asciiImgPathList [1]). Size videoWritter= VideoWriter (savePath, videoFourcc, vp.get (cv2.CAP_PROP_FPS) AsciiImgTemp) for imagePath in asciiImgPathList: videoWritter.write (cv2.imread (imagePath)) videoWritter.release () 4 complete code import cv2 from PIL import Image,ImageFont,ImageDrawimport osfrom cv2 import VideoWriter, VideoWriter_fourcc'''* @ breif: convert pixel color to ASCII character * @ param [in]: pixel RGBA value * @ retval: character''def color2Char . ") If alpha: gray = int (0.2126 * r + 0.7152 * g + 0.0722 * b) unit = 0.2126 / len (imgChar) return imgChar [int (gray / unit)] else: return'* @ breif: convert video frame by frame to picture * @ paramin: vp-> openCV video handle * @ retval: number-> number of pictures converted''def video2Pic (vp) : number = 0 if vp.isOpened (): r Frame = vp.read () if not os.path.exists ('cachePic'): os.mkdir (' cachePic') os.chdir ('cachePic') else: r = False while r: number + = 1 cv2.imwrite (str (number) +' .jpg', frame) r Magi frame = vp.read () os.chdir ("..") Return number''* @ breif: convert a picture pixel by pixel to the ASCII character * @ param [in]: imagePath-> image path * @ param [in]: index-> image index * @ retval: None'''def img2Char (imagePath, index): # initialize txt, color, font ='', [], ImageFont.load_default () .font imgWidth, imgHeight = Image.open (imagePath). Size asciiImg = Image.new ("RGB", (imgWidth, imgHeight) (255255255) drawPtr = ImageDraw.Draw (asciiImg) imgWidth, imgHeight = int (imgWidth / 6), int (imgHeight / 15) # convert image frames to ASCII characters pixel by pixel and record RGB values img = Image.open (imagePath) .convert ('RGB'). Resize ((imgWidth, imgHeight), Image.NEAREST) for i in range (imgHeight): for j in range (imgWidth): pixel = img.getpixel ((j) I) color.append ((pixel [0], pixel [1], pixel [2])) txt = txt + color2Char (pixel [0], pixel [1], pixel [2], pixel [3]) if len (pixel) = = 4 else\ txt + color2Char (pixel [0], pixel [1]) Pixel [2]) txt + ='\ n' color.append ((255255255)) # draw ASCII characters and save x, y = 0for i in range 0 fontW, fontH = font.getsize (txt [1]) fontH * = 1.37for i in range (len (txt)): if (txt [I] = ='\ n'): X + = fontH y =-fontW drawPtr.text ((YPowerX) Txt [i] Fill=color [I]) y + = fontW os.chdir ('cacheChar') asciiImg.save (str (index) +' .jpg') os.chdir ("..")'* @ breif: convert video to ASCII image set * @ param [in]: number-> number of frames * @ retval: None''' def video2Char (number): if not os.path.exists ('cacheChar'): os.mkdir (' cacheChar) ') img_path_list = [' cachePic' + rpm / {} .jpg '.format (I) for i in range (1) Number + 1)] task = 0 for imagePath in img_path_list: task + = 1 img2Char (imagePath, task)''* @ breif: synthesize images into video * @ paramin]: vp-> openCV video handle * @ paramin]: number-> number of frames * @ paramin]: savePath-> video save path * @ retval: None''' def img2Video (vp, number) SavePath): videoFourcc = VideoWriter_fourcc (* "MP42") # set video encoder asciiImgPathList = ['cacheChar' + rpm / {} .jpg' .format (I) for i in range (1, number + 1)] asciiImgTemp = Image.open (asciiImgPathList [1]). Size videoWritter= VideoWriter (savePath, videoFourcc, vp.get (cv2.CAP_PROP_FPS) AsciiImgTemp) for imagePath in asciiImgPathList: videoWritter.write (cv2.imread (imagePath)) videoWritter.release () if _ _ name__ ='_ _ main__': videoPath = 'test.mp4' savePath =' new.avi' vp = cv2.VideoCapture (videoPath) number = video2Pic (vp) video2Char (number) img2Video (vp, number, savePath) vp.release () Thank you for reading! This is the end of the article on "how to use Python code to make dynamic firecrackers". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.