Programming the dog

Programming Daniu technology sharing platform

\

preface

In people’s image, programmers are highly intelligent, but at the same time dull. Today I will teach you how to SAO up, can be said to learn today’s tutorial, sent to the circle of friends, very cool. Python3 is a program that reads a video in mp4 format and outputs it as a terminal. Finally present the effect, really quite SAO qi. We can take a look at the final video first. \

Class =”video_iframe” data-vidType =”2″ allowFullScreen =”” frameborder=”0″ data-ratio=”1.7058823529411764″ data-w=”464″ Scrolling = “no” data – SRC = “v.qq.com/iframe/play…” style=”display: block; width: 638px ! important; Height: 358.875 px! important;” Width = “638” height = “358.875” data – the vh = “358.875” data – vw = “638” SRC = “http://v.qq.com/iframe/player.html?vid=s1336td6qdq&width=638&height=358.875&auto=0” / > \

preparation

This program has three code files: getiamge. py, image2txt,py, play.py, two folders for images and text, and an MP4 video. Make sure you have installed the PIL and NUMpy third-party libraries and the FFMPEG plugin before running.

Project file structure

PIL and Numpy can be installed via PIP. But ffmpeg can go to download website (www.ffmpeg.org/download.ht…

Ffmpeg installation

\

To install FFmPEG, right click on My Computer – Properties – Advanced System Settings – Environment variable -Path and add “;d:\ FFmpeg \bin “(your own installation Path).

getImage.py

Once the required third library and FFMPEG are installed, you can start running Python code. The first thing to run is getimage.py. Getimage. py is used to extract frames from videos. The extracted frames are stored in the “Images/” folder. If frames already exist in the folder, do not run this program. To adjust the cumulative value of crop_time, clear the existing image set, modify the videoPath “videoPath” and frame picture directory path “imagePath”, and then run getimage.py.

The getImage. Py code:

Import OS def getImage(videoPath, imagePath): img_count = 1 crop_time = 0.0 while crop_time <= 15.0: os.system('ffmpeg -i %s -f image2 -ss %s -vframes 1 %s.png' % (videoPath, str(crop_time), imagePath + str(img_count))) img_count += 1 print('Geting Image ' + str(img_count) + '.png' + ' from time ' + STR (crop_time)) crop_time += 0.1 print('Image Collected') if __name__ == '__main__': videoPath = 'E:/badapple/a/1.mp4' imagePath = 'E:/badapple/a/images/' getImage(videoPath, imagePath)Copy the code

image2txt.py

Image2txt. py is used to convert an image into a character drawing of a certain size, extract the image from the “images/” folder, convert it into TXT text and store it in the “TXT/” folder. The default terminal size is 100×50. If the screen does not support this size, you can change the charWidth parameter to the number of characters displayed on each line. After the adjustment, if you run play.py, the terminal size is (charWidth)x(charWidth/2). Parameter value, there is no fixed value, you can try more.

Note: running this file alone has no effect, because the getImage function is defined in this file, not called. The specific invocation is in “play.py”.

Code:

from PIL import Image
import numpy 
def image2txt(inputFile, outputFile):
    im = Image.open(inputFile).convert('L')
    charWidth = 100
    im = im.resize((charWidth, charWidth // 2))
    target_width, target_height = im.size
    data = numpy.array(im)[:target_height, :target_width]
    f = open(outputFile, 'w',encoding='utf-8')
    for row in data:
        for pixel in row:
            if pixel > 127:
                f.write('1')
            else:
                f.write(' ')
        f.write('\n')
    f.close()
Copy the code

play.py

Play. py is used to get the TXT text set and display the TXT text set in sequence to play character animation on the terminal. Before running this command, modify the video directory videoPath, TXT folder txt_dir_path, and images folder img_dir_path. Then run play.py.

Note: if you are running a MAC OS, you also need to change the “type” in the code to “cat”, leaving Windows unchanged. That is:

os.system('cat ' + txtPath + str(txt_count) + '.txt')
Copy the code

* * * *

Appreciate the author


\

Author: Crazy sea, knowledge and cognition, wealth and thinking. Share Python knowledge and knowledge.

Recent Hot articles

Using Python to analyze Apple stock price data \

Nginx+ UWSGi to deploy Django applications \

Using text mining to analyze nearly 50,000 Poems of the Whole Tang Dynasty \

Python natural Language Processing analysis: How to kill dragons \

Python 3.6 enables individual bloggers to crawl their tweets, images, and comments

Click below to read the original article and become a community member for freeCopy the code