Home  >  Article  >  Backend Development  >  How to open, display, and save images using python

How to open, display, and save images using python

不言
不言Original
2018-05-04 14:08:576177browse

This article mainly introduces the method of opening, displaying and saving images with python. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

When it comes to digital image processing, most people may think of matlab, but matlab also has its own shortcomings:

1. It is not open source and expensive.

2. The software has large capacity. Generally, it is 3G or above, and higher versions can even reach 5G or above.

3. It can only be used for research and is not easy to convert into software.

Therefore, we use the scripting language python here for digital image processing.

To use python, you must first install python, usually version 2.7 or above. Whether it is a windows system or a linux system, the installation is very simple.

To use python for various development, you must install the corresponding library. This is very similar to matlab, except that it is called a toolbox in matlab, and it is called a library or package in python. To install these libraries, you usually use pip to install them.

To use python for digital image processing, you must also install the Pillow package. Although python comes with a PIL (python images library), this library has stopped being updated, so Pillow is used, which is developed from PIL.

pip install Pillow

1. Opening and displaying pictures

from PIL import Image
img=Image.open('d:/dog.png')
img.show()

Although Pillow is used, it is forked from PIL, so it still needs to be imported from PIL. Use the open() function to open the image, and use the show() function to display the image.

This way of displaying pictures is to call the image browser that comes with the operating system to open the picture. Sometimes this method is not convenient, so we can also use another method to let the program draw the picture. .

from PIL import Image
import matplotlib.pyplot as plt
img=Image.open('d:/dog.png')
plt.figure("dog")
plt.imshow(img)
plt.show()

Although this method is a bit more complicated, it is recommended to use this method. It uses a matplotlib library to draw pictures for display. matplotlib is a professional drawing library, equivalent to plot in matlab. You can set multiple figures, set the title of the figure, and even use subplot to display multiple pictures in one figure. matplotlib can be installed directly

pip install matplotlib

##figure defaults to axis, if not needed, we can turn it off

plt.axis('off')

After opening the picture, you can use some attributes to view the picture information, such as

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

display The result is:

(558, 450)

RGBA
PNG

2. Save the picture

img.save('d:/dog.jpg')

Just one line of code, very simple. This line of code can not only save the image, but also convert the format. In this example, the original png image is saved into a jpg image.

Related recommendations:

Use python to process images to achieve pixel access in images

##

The above is the detailed content of How to open, display, and save images using python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn