Home >Backend Development >Python Tutorial >Python implements two methods of reading and displaying images

Python implements two methods of reading and displaying images

高洛峰
高洛峰Original
2017-01-14 13:26:552892browse

In addition to using opencv in python, you can also use matplotlib and PIL to operate images. I prefer matpoltlib because its syntax is more like matlab.

1. matplotlib

1. Display the picture

import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
import numpy as np
 
lena = mpimg.imread('lena.png') # 读取和代码处于同一目录下的 lena.png
# 此时 lena 就已经是一个 np.array 了,可以对它进行任意处理
lena.shape #(512, 512, 3)
 
plt.imshow(lena) # 显示图片
plt.axis('off') # 不显示坐标轴
plt.show()


2. Display a channel

# 显示图片的第一个通道
lena_1 = lena[:,:,0]
plt.imshow('lena_1')
plt.show()
# 此时会发现显示的是热量图,不是我们预想的灰度图,可以添加 cmap 参数,有如下几种添加方法:
plt.imshow('lena_1', cmap='Greys_r')
plt.show()
 
img = plt.imshow('lena_1')
img.set_cmap('gray') # 'hot' 是热量图
plt.show()

3. Convert RGB to grayscale image

There is no suitable function in matplotlib to convert RGB image to grayscale image. You can customize one according to the formula:

def rgb2gray(rgb):
  return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
 
gray = rgb2gray(lena) 
# 也可以用 plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.imshow(gray, cmap='Greys_r')
plt.axis('off')
plt.show()

4. Scale the image

Scipy is used here

from scipy import misc
lena_new_sz = misc.imresize(lena, 0.5) # 第二个参数如果是整数,则为百分比,如果是tuple,则为输出图像的尺寸
plt.imshow(lena_new_sz)
plt.axis('off')
plt.show()

5. Save the image

5.1 Save the image drawn by matplotlib

This method is suitable for saving any matplotlib drawn The image is equivalent to a screencapture.

plt.imshow(lena_new_sz)
plt.axis('off')
plt.savefig('lena_new_sz.png')

5.2 Save the array as an image

from scipy import misc
misc.imsave('lena_new_sz.png', lena_new_sz)

5.3 Save the array directly

After reading, you can still display the image according to the previous method of displaying the array. This method is completely No loss of image quality

np.save('lena_new_sz', lena_new_sz) # 会在保存的名字后面自动加上.npy
img = np.load('lena_new_sz.npy') # 读取前面保存的数组

2. PIL

1. Display the image

from PIL import Image
im = Image.open('lena.png')
im.show()

2. Convert the PIL Image image into a numpy array

im_array = np.array(im)
# 也可以用 np.asarray(im) 区别是 np.array() 是深拷贝,np.asarray() 是浅拷贝

3. Save the PIL image

Directly call the save method of the Image class

from PIL import Image
I = Image.open('lena.png')
I.save('new_lena.png')

4. Convert the numpy array to the PIL image

Here we use matplotlib.image to read the image array , note that the array read here is of type float32, the range is 0-1, while the PIL.Image data is of type uinit8, the range is 0-255, so conversion is required:

import matplotlib.image as mpimg
from PIL import Image
lena = mpimg.imread('lena.png') # 这里读入的数据是 float32 型的,范围是0-1
im = Image.fromarray(np.uinit8(lena*255))
im.show()

5. RGB conversion For the grayscale image

from PIL import Image
I = Image.open('lena.png')
I.show()
L = I.convert('L')
L.show()

The above is the entire content of this article. I hope it will be helpful to everyone's learning, and I also hope that everyone will pay more attention to the PHP Chinese website.

For more related articles on two methods of reading and displaying images in python, please pay attention to 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