Some personal understanding of the tutorial steps
1. Install the Python development environment and toolkit pillow(PIL)
1.1 what PIL:
– Python Imagine Library is a common image processing toolkit for Python.
1.2. What is a Pillow?
-PIL library only supported Python2.7 before it stopped updating, then someone changed some bugs in PIL based on the PIL code and continued to update to support Python3.x. Pillow is an improved PIL library.
1.3 Why do we install pillow(PIL) :
– After we install pillow, we can use the specific methods written in the toolkit to do the image processing in our code without having to implement the image processing ourselves.
1.4 The code for installing PIL means:
-sodu is for obtaining administrator permission, apt-get is for updating, installing, and uninstalling software in Linux system. Generally, root permission is required, so sudo is added before this command header
sudo apt-get update
Copy the code
– Update the software package list. The purpose is to obtain the latest version of the software you want to install, it is best to update before each installation, otherwise the installation software may be wrong
sudo apt-get install python-dev
Copy the code
Install the python-dev package.
– Why pack this bag:
Linux distributions typically split the class library header and the associated PKG-config into a separate XXX-dev (EL) package
If we want to use Python commands, we still need to add this library to the default development environment of the lab building.
sudo apt-get install libtiff5-dev libjpeg8-dev ...... python-tkCopy the code
– Install some image-related dependencies for the pillow.
sudo pip install pillow
Copy the code
– Install the Pillow toolkit with the PIP manager
Now that the Python environment and the Pillow toolkit are installed, you are ready to try writing the.py script for image-to-character drawing.
2. Write the conversion script ASCIi.py
2.1. Obtain the image to be converted
wget http://labfile.oss.aliyuncs.com/courses/370/ascii_dora.png
Copy the code
– Download the image to be converted from that HTTP address
2.2. Write conversion scripts
– First, create the script file
>vi ascii.py
Copy the code
This is a vim command, which has the same meaning as creating an ASCII. TXT file with Notepad on Windows, except because it’s a Python script, the file type suffix is.py.
So, if you don’t know what VIM is and how to use it, please refer to the VIM tutorial in the lab building
– Then start writing the conversion code, and we’ll explain what the code means
sfrom PIL import Image
import argparse
Copy the code
Reference PIL’s Image library and the Argparse module from the Python standard library (see the argparse tutorial). Once referenced, you can use the library and module methods directly in the following code.
Parser = argparse.argumentParser () parser. Add_argument ('file') # Input file parser. Add_argument ('-o', Add_argument ('--width', type = int, default = 80) # Parser. add_argument('--height', parser) Type = int, default = 80) #Copy the code
The above code means: There are four arguments that can be entered on the command line: file(address of the file to be converted), -o(address of the file to be converted to save the character drawing), –width(width of the output character drawing), and –height(height of the output character drawing). If you have followed Argparse’s simple tutorial, you know what this means. Let me start by saying that the last command we executed after writing the script was:
$ python ascii.py ascii_dora.png
Copy the code
The subsequent parameter ascii_dora.png is the value of the file parameter. By default, the width and height of the output file are 80. By default, the output address is “output.txt”.
Parse_args () IMG = args. File WIDTH = args. WIDTH HEIGHT = args. HEIGHT OUTPUT = argsCopy the code
Get the value of the input parameter, nothing to say.
Parse_args () IMG = args. File WIDTH = args. WIDTH HEIGHT = args. HEIGHT OUTPUT = argsCopy the code
Def get_char is a method that converts the color of an image to a character. (I don’t know exactly why.) But what this method means is to convert a color value to the corresponding character and return the converted character
">if __name__ == '__main__':
Copy the code
Start executing the code after the colon
!!!!!!!!! Notice, now we’re going to do something with the image
>im = Image.open(IMG)
im = im.resize((WIDTH,HEIGHT), Image.NEAREST)
Copy the code
Open the entered image and resize it
txt = ""
for i in range(HEIGHT):
for j in range(WIDTH):
txt +=get_char(*im.getpixel((j,i)))
txt += '\n'
print txt
Copy the code
Get_char () = get_char() = get_char() = get_char() = get_char
In fact, at this point, the code has completed the function of converting the image to character painting (will output the result in the terminal), and the following point is to output the character painting to a local file
If OUTPUT: with open(OUTPUT,'w') as f: f.write(TXT) else: with open("output.txt",'w') as f: f.write(TXT)Copy the code
If the output file address is given, the string is saved to the given file. If not, save to the file “output.txt”.
2.3 run the conversion script you just wrote
>python ascii.py ascii_dora.png
Copy the code
The effect is the same as the picture in the tutorial, and Doraemon becomes character painting Doraemon. But the effect is not as good as in the tutorial, I guess the tutorial code conversion function is written poorly, some Settings are not set in place, resulting in.
I later found a picture of a small train by myself, and the effect after conversion is shown as follows:
The results of the completed experiment