When it comes to digital image processing, perhaps most people will think of MATLAB, but MATLAB has its own shortcomings:
1, not open source, the price is expensive
2, the software capacity is large. Generally more than 3G, high version even up to 5G or more.
3, can only do research, not easily converted into software.
Therefore, we use the Python scripting language here to do digital image processing.
To use Python, you must first install Python, which is typically 2.7 or more, and is easy to install, whether it's on a Windows system or a Linux system.
To use Python for various development, you must install the corresponding library. This is very similar to MATLAB, but Matlab is called the Toolbox, and Python is called a library or a package. Installing these libraries is typically installed using PIP.
Using Python for digital picture processing, you have to install the pillow package. Although Python comes with a PIL (Python images Library), the library has now stopped updating, so using pillow, it was developed by PIL.
Pip Install Pillow
First, the opening and display of pictures
from Import imageimg=image.open ('d:/dog.png') img.show ()
Although pillow is used, it is PIL fork, so you still want to import from PiL. Open the picture using the open () function, and use the show () function to display the picture.
This kind of image display is to call the operating system's own image browser to open the picture, sometimes this way is not very convenient, so we can also use another way, let the program to draw pictures.
from Import Image Import Matplotlib.pyplot as Pltimg=image.open ('d:/dog.png') plt.figure ( "dog") Plt.imshow (IMG) plt.show ()
This approach is more complex, but it is recommended to use a matplotlib library to draw images for display. Matplotlib is a library of professional drawings, equivalent to plot in MATLAB, where you can set up multiple figures, set the caption of a figure, and even use subplot to display multiple images in a graph. Matplotlib can be installed directly
Pip Install Matplotlib
Figure by default is with axis, if no need, we can turn off
Plt.axis ('off')
After you open the picture, you can use some properties to view the picture information, such as
print img.size # picture size print img.mode # picture mode format of print Img.format # picture
The results shown are:
(558, 450) Rgbapng
Second, the preservation of the picture
Img.save ('d:/dog.jpg')
Just one line of code, very simple. This line of code can not only save the picture, or the conversion format, as in this example, the original PNG image is saved in order to JPG pictures.
Python of Digital image processing one: open \ display \ Save Image