scipy.misc.imread()函数解析(最清晰的解释)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/TeFuirnever/article/details/90802269

scipy.misc.imread()函数用于从文件中读取图像作为数组。

更多的读取图片的方法可以看这个博客——【超分辨率】各种python图像库的图片读取方法总结
scipy.misc.imread(name, 
	flatten=False, 
	mode=None
)

参数:

  • name:str或file对象。要读取的文件名或文件对象。

  • flatten:bool,可选。如果为True,则将颜色层展平为单个灰度图层。

  • mode:str,可选。将图像转换为例如的模式’RGB’。

返回:

  • imread:ndarray。通过读取图像获得的阵列。

注意:

   """
    Notes
    -----
    `imread` uses the Python Imaging Library (PIL) to read an image.
    The following notes are from the PIL documentation.
    `mode` can be one of the following strings:
    * 'L' (8-bit pixels, black and white)
    * 'P' (8-bit pixels, mapped to any other mode using a color palette)
    * 'RGB' (3x8-bit pixels, true color)
    * 'RGBA' (4x8-bit pixels, true color with transparency mask)
    * 'CMYK' (4x8-bit pixels, color separation)
    * 'YCbCr' (3x8-bit pixels, color video format)
    * 'I' (32-bit signed integer pixels)
    * 'F' (32-bit floating point pixels)
    PIL also provides limited support for a few special modes, including
    'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa'
    (true color with premultiplied alpha).
    When translating a color image to black and white (mode 'L', 'I' or
    'F'), the library uses the ITU-R 601-2 luma transform::
        L = R * 299/1000 + G * 587/1000 + B * 114/1000
    When `flatten` is True, the image is converted using mode 'F'.
    When `mode` is not None and `flatten` is True, the image is first
    converted according to `mode`, and the result is then flattened using
    mode 'F'.
    """

以下是翻译:

imread使用Python Imaging Library(PIL)读取图像。以下注释来自PIL文档

mode可以是以下字符串之一:

  • ‘L’(8位像素,黑白)
  • ‘P’(8位像素,使用调色板映射到任何其他模式)
  • ‘RGB’(3x8位像素,真彩色)
  • ‘RGBA’(4x8位像素,带透明蒙版的真彩色)
  • ‘CMYK’(4x8位像素,分色)
  • ‘YCbCr’(3x8位像素,彩色视频格式)
  • ‘I’(32位有符号整数像素)
  • ‘F’(32位浮点像素)

PIL还为一些特殊模式提供有限的支持,包括’LA’(带有alpha的’L’),‘RGBX’(带填充的真彩色)和’RGBa’(带有预乘alpha的真彩色)。

将彩色图像转换为黑白(模式“L”,“I”或“F”)时,库使用ITU-R 601-2亮度转换:

L = R 299 / 1000 + G 587 / 1000 + B 114 / 1000 L=R*299/1000+G*587/1000+B*114/1000

flatten为True时,使用mode“F”转换图像。当mode不是None并且flatten为True时,首先根据mode转换图像,然后使用mode’F’flatten结果。

例子:

from PIL import Image
import scipy.misc

a = Image.open('1.jpg')
d = Image.open('1.jpg').convert('RGB')
print(type(a))
print(type(d))
print(a)
print(d)

b = scipy.misc.imread('1.jpg')
e = scipy.misc.imread('1.jpg',mode='RGB')
print(type(b))
print(type(e))
print(b)
print(e)
<class 'PIL.JpegImagePlugin.JpegImageFile'>
<class 'PIL.Image.Image'>
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x336 at 0x1F3D1C406A0>
<PIL.Image.Image image mode=RGB size=500x336 at 0x1F3C9879E48>

<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
[[[221 227 225]
  [222 226 225]
  [220 224 223]
  ...
  ...
  [ 27  21   0]
  [ 12   7   0]
  [ 11   8   0]]]
  
[[[221 227 225]
  [222 226 225]
  [220 224 223]
  ...
  ...
  [ 27  21   0]
  [ 12   7   0]
  [ 11   8   0]]]

可以看出scipy.misc.imread()函数返回的是numpy.ndarray

猜你喜欢

转载自blog.csdn.net/TeFuirnever/article/details/90802269