If a programmer doesn’t suck, what kind of programmer

20180406

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. Today I’m going to teach you python3, 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 output first.

Video link

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. Can go to download website (https://www.ffmpeg.org/download.html#build-windows) and ffmpeg. Open the official website corresponding to your system, click the icon below, to download.

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.0whileCrop_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.1print('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

Write in the last

I have uploaded the corresponding program source code and video files to the public account “Crazy Sea”, you can just reply “Python code” in the background. If you think this tutorial is good, please like it and forward it to your moments. Finally, download the code, run it, record a video and post it on moments.

Programmers can also coquettish up!