Python simple picture Processing (1): Open \ Show \ Save Picture __python

Source: Internet
Author: User
Tags install matplotlib

A reference to Digital image processing, may most people think of MATLAB, but MATLAB has its own shortcomings:
1. Not open source, the price is expensive;
2. Large software capacity. Generally more than 3GB, the high version even reached 5GB or more.
3. Easy to do research, not easily converted into software.

Therefore, I tend to learn python to do image processing work. To use Python, the first thing to do is to install Python, which is typically above version 2.7, whether it's in Windows or Linux, and the installation work is very simple.

To use Python for all kinds of development, we have to install the corresponding libraries or. Just like the toolbox we installed in MATLAB (Toolbox). Install these libraries, typically using the PIP.

Using Python for digital image processing, you also have to install the pillow package. Although Python has a PIL (Python image Library), but this library has now stopped updating, so using pillow, it was developed by PIL.

Pip Install Pillow

Because I installed the anaconda, so save this step. With regard to anaconda, a topic will be written in a later section.
Use the PIP list to view the existing installation packages in Anaconda and find that there are already pillow.
(Don't repeat this condition later)

Opening and displaying of a picture

From PIL import Image
img=image.open (' e:/a.png ')
img.show ()

Although the use of pillow, but it is from the PiL fork, so still want to import from the PiL. Use the open () function to open the picture and use the show () function to display the picture.

The above method is sometimes inconvenient, so we can also use the other way, let the program to draw the picture.

From PIL import Image
import matplotlib.pyplot at Plt
img=image.open (' e:/a.png ')
plt.figure ("a")
Plt.imshow (IMG)
plt.show ()

Although this approach is a bit more complicated, it is recommended that you use a matplotlib library to draw pictures for display. Matplotlib is a professional drawing library, equivalent to the plot in MATLAB, you can set up a number of figure, set figure title, can even use subplot in a figure display more than one picture.

Matplotlib can be installed directly

Pip Install Matplotlib

Figure the default is axis, we can turn it off if there is no need.

Plt.axis (' off ')

After you open a picture, you can use some properties to view picture information, such as:

Print img.size #图片尺寸
print img.mode #图片模式
print Img.format #图片格式

Display results as:
(690, 1226)
Rgb
Jpeg

Preservation of two pictures

Img.save (' e:/b.jpg ')

On one line of code, this line of code not only saves pictures, but also converts formatting. This time, the original PNG format is saved in JPG format.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.