当前位置:   article > 正文

python中读取和查看图片的6种方法_python读取图片

python读取图片

file_name1='test_imgs/spect/1.png' # 这是彩色图片
file_name2='test_imgs/mri/1.png' # 这是灰度图片
  • 1
  • 2
'
运行

在这里插入图片描述

1 OpenCV

注:用cv2读取图片默认通道顺序是B、G、R,而不是通常的RGB顺序,所以读进去的彩色图直接显示会出现变色情况,详情可以看:https://blog.csdn.net/weixin_45954454/article/details/114707888

import cv2
spect= cv2.imread(file_name1) # BGR
spect= spect[:, :, ::-1] # RGB
mri= cv2.imread(file_name2) # 灰度图
print(spect.shape) # (256, 256, 3)
print(mri.shape) # (256, 256, 3)   cv2读进来是三通道的图片
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
import matplotlib.pyplot as plt
plt.imshow(spect)
plt.show()
  • 1
  • 2
  • 3

在这里插入图片描述

import matplotlib.pyplot as plt
fig=plt.figure()
f1 = fig.add_subplot(121)
f2 = fig.add_subplot(122)
f1.imshow(spect)
f2.imshow(mri)
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

2 imageio

import imageio 
spect = imageio.imread(file_name1) 
mri = imageio.imread(file_name2) 
print(spect.shape) # (256, 256, 3)
print(mri.shape) # (256, 256)
  • 1
  • 2
  • 3
  • 4
  • 5
import matplotlib.pyplot as plt
fig=plt.figure()
f1 = fig.add_subplot(121)
f2 = fig.add_subplot(122)
f1.imshow(spect)
f2.imshow(mri,cmap='gray') # 注:单通道灰度图必须加上cmap='gray'才能正确显示
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

3 PIL

from PIL import Image
import numpy as np
spect= Image.open(file_name1) #  <PIL.PngImagePlugin.PngImageFile image mode=RGB size=256x256 at 0x1D9F15FFDC8>
spect.show()
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

4 scipy.misc

from scipy.misc import imread
spect = imread(file_name1)
mri = imread(file_name2)
  • 1
  • 2
  • 3
import matplotlib.pyplot as plt
fig=plt.figure()
f1 = fig.add_subplot(121)
f2 = fig.add_subplot(122)
f1.imshow(spect)
f2.imshow(mri,cmap='gray') # 注:单通道灰度图必须加上cmap='gray'才能正确显示
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

5 tensorflow

from tensorflow.python.keras.preprocessing.image import load_img
spect = load_img(file_name1) #  <PIL.PngImagePlugin.PngImageFile image mode=RGB size=256x256 at 0x1D9EF188048>
  • 1
  • 2
spect.show()
  • 1

在这里插入图片描述

6 skimage

from skimage import io
import matplotlib.pyplot as plt
mri = io.imread(file_name2)#读取数据
plt.imshow(mri,cmap='gray') # 注:单通道灰度图必须加上cmap='gray'才能正确显示
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/822899
推荐阅读
相关标签
  

闽ICP备14008679号