This article is participating in Python Theme Month. See the link for details

Author: Flower Gie

Wechat official account: Java development zero to one

preface

Yesterday saw the news about the party’s one hundredth birthday, don’t blow not black, flower govemment is excited to the career, the one hundred, we are too not easy, this is the countless ancestors with the life, as a contemporary youth, friends, be sure to cherish this life without regret into the mantra of the Chinese this sentence is a lot of friend, spend govemment love too! For the motherland, we forge ahead!

So I thought of the idea of an article I had read before, and I thought I would output the video into animation to see the effect.

Here pretend to have a video =_=, see a very cool video,….

P6-juejin.byteimg.com/tos-cn-i-k3…

The body of the

Smoke video frames

I have done video editing for some time before, so I am quite skilled in using Pr (Adobe Premiere Pro 2020), and I am proud of my versatility.

To export the edited video, the steps are as follows: Click File -> Export -> Media. I selected JPEG format here, and 586 pictures were generated.

Convert pictures to ASCII characters

This step took me a long time, because Baidu did not find a suitable tool for conversion for about an hour, so I wrote a tool class in Python.

from PIL import Image
import os

table = '#8XOHLTI)i=+; :,. '
path = R 'Image directory'
for filename in os.listdir(path):
    if filename.find(".txt") != -1:
        continue
    im = Image.open(os.path.join(path, filename))
    ifim.mode ! ="L":  
        im = im.convert("L")
    a = im.size[0]
    b = im.size[1]
    destName = os.path.join(path, filename).replace(".jpg".".txt")
    f = open(destName, 'w+') 

    for i in range(1, b, 2): 
        line = ' '
        for j in range(a):
            line += table[int((float(im.getpixel((j, i))) / 256.0) * len(table))] characters line +="\n" 
        f.write(line)
    f.close()
Copy the code

After the implementation of this tool class, you can convert more than 500 pictures into TXT files.

Synthesis into a TXT total file

Because when we play the video, the generated hundreds of TXT files, there is a problem here, because the original video upper and lower parts of the black box, so converted TXT also have a large number of redundant characters, so merge, specifically for this part of the processing, merge code as follows.

from PIL import Image
import os

line_all = ' '
table = '#8XOHLTI)i=+; :,. '
path = r'TXT folder path'
for filename in os.listdir(path):
    if filename.find(".jpg") != -1:
        continue
    with open(os.path.join(path, filename)) as file_obj:
        index = 1
        line = ' '
        for content in file_obj:
            if 137 < index < 343: // Keep only rows 137 to 343
                line += content + "\n"
            index = index + 1
        line_all += line
destName = "Merge total file path"
f = open(destName, 'w+')
f.write(line_all)
f.close()

Copy the code

GUI interface writing

Plus is using tkinter sent total TXT file to output characters, I find the video of this time in after the treatment, each the size of the screen for 410 lines, in order to the size of the compatible with my computer, so adjusted the font size, friends diy also according to the actual situation to adjust, tahoma font choice, the code is as follows.

import tkinter as tk
import os
import tkinter.font as tf

class Questions(tk.Tk):
    def __init__(self, *args, **kw):
        super(a).__init__(a)
        self.wm_title('1921-2021')
        self.configure(background='white')
        self.wm_minsize(1200.1400)# set window minimize size self.wm_maxsize(1200.1400)# set window maximum size self.resizable(width=False, height=True)# Set window width immutable, height variable ft= tf.Font(family='宋体', size=1Self. text = tk. text (self,width= tk. text1200, height=300,font = ft)
        # self.text.tag_add('tag1'.'1.10'.'1.12')
        # self.text.tag_config('tag1', background='yellow', foreground='red')
        self.text.pack()

        self.index = 1
        self.run()
        self.refresh_data()
        self.mainloop()

    def refresh_data(self):
        path = r'TXT merge folder directory'
        with open(os.path.join(path, "TXT file name")) as file_obj:
            self.index = self.index +1
            line = ' '
            for content in file_obj:
                if self.index % 410= =0:
                    self.text.delete(1.0, tk.END)
                    self.text.insert(1.410, line)
                    line = ' '
                self.index = self.index + 1
                line = line + content + "\n"
        self.after(50, self.refresh_data) #50Brush the screen once in millisecondsdef run(self):
        pass


if __name__ = ='__main__':
    question = Questions()
Copy the code

Here are a few things to look out for, all of which Gie stepped on:

  • The width and height of the window, the width and height of the text area and the font size should be proportional to each other, otherwise the effect cannot be seen
  • Windows system, select the font of Song Style; For Mac, select Andale Mono
  • I tried a lot of things at first, but the text just didn’t refresh in real time, righttkinterNot familiar with, finally use the current after call itself according to a certain frequency to achieve
  • The number of lines in each output is the number of lines in the original single file
  • View TXT file is, use Ctrl+ scroll axis to reduce the font with, you can view the effect

Results show

conclusion

After reading the above, it is estimated that there will be many small partners in the heart think, this above things I will, too pediatric. If you think the same way, I will definitely ask for advice humbly. As middle-aged and old people in the new era, we should take the responsibility and realize the relay! Come on! Guys.

The above is the whole content of this issue, if there is a mistake, also ask friends to leave a message to correct. I’m Gie, feel free to leave a comment if you have any questions, and we’ll see you next time at 🦮.

The article continues to update, you can wechat search “Java development zero to one” for the first time to read, the follow-up will continue to update Java interview and all kinds of knowledge points, interested partners welcome to pay attention to, learn together, ha 🐮🥃.

Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!