繁体   English   中英

如何将灰度图像转换为像素值列表?

[英]How to convert a grayscale image into a list of pixel values?

我正在尝试创建一个 python 程序,它采用灰度、24*24 像素的图像文件(我尚未决定类型,因此欢迎提出建议)并将其转换为从 0(白色)到 255 的像素值列表(黑色)。

我计划使用这个数组来创建一个类似于MNIST 的图片字节文件,它可以被 Tensor-Flow 手写识别算法识别。

我发现Pillow 库在此任务中最有用,它遍历每个像素并将其值附加到数组

from PIL import Image

img = Image.open('eggs.png').convert('1')
rawData = img.load()
data = []
for y in range(24):
    for x in range(24):
        data.append(rawData[x,y])

然而这个解决方案有两个问题:

  1. 像素值不存储为整数,而是无法进一步数学操作的像素对象,因此无用。
  2. 甚至Pillow 文档也指出:

    访问单个像素相当慢。 如果您要遍历图像中的所有像素,则使用 Pillow API 的其他部分可能会更快。

您可以将图像数据转换为 Python 列表(或列表列表),如下所示:

from PIL import Image

img = Image.open('eggs.png').convert('L')  # convert image to 8-bit grayscale
WIDTH, HEIGHT = img.size

data = list(img.getdata()) # convert image data to a list of integers
# convert that to 2D list (list of lists of integers)
data = [data[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)]

# At this point the image's pixels are all in memory and can be accessed
# individually using data[row][col].

# For example:
for row in data:
    print(' '.join('{:3}'.format(value) for value in row))

# Here's another more compact representation.
chars = '@%#*+=-:. '  # Change as desired.
scale = (len(chars)-1)/255.
print()
for row in data:
    print(' '.join(chars[int(value*scale)] for value in row))

这是我用于测试的小型 24x24 RGB eggs.png图像的放大版本:

放大版eggs.png

这是第一个访问示例的输出:

测试图像的屏幕截图输出

这是第二个示例的输出:

@ @ % * @ @ @ @ % - . * @ @ @ @ @ @ @ @ @ @ @ @
@ @ .   . + @ # .     = @ @ @ @ @ @ @ @ @ @ @ @
@ *             . .   * @ @ @ @ @ @ @ @ @ @ @ @
@ #     . .   . .     + % % @ @ @ @ # = @ @ @ @
@ %       . : - - - :       % @ % :     # @ @ @
@ #     . = = - - - = - . . = =         % @ @ @
@ =     - = : - - : - = . .     . : .   % @ @ @
%     . = - - - - : - = .   . - = = =   - @ @ @
=   .   - = - : : = + - : . - = - : - =   : * %
-   .   . - = + = - .   . - = : - - - = .     -
=   . : : . - - .       : = - - - - - = .   . %
%   : : .     . : - - . : = - - - : = :     # @
@ # :   .   . = = - - = . = + - - = - .   . @ @
@ @ #     . - = : - : = - . - = = : . .     # @
@ @ %     : = - - - : = -     : -   . . .   - @
@ @ *     : = : - - - = .   . - .   .     . + @
@ #       . = - : - = :     : :   .   - % @ @ @
*     . . . : = = - : . .   - .     - @ @ @ @ @
*   . .       . : .   . .   - = . = @ @ @ @ @ @
@ :     - -       . . . .     # @ @ @ @ @ @ @ @
@ @ = # @ @ *     . .     . - @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ .   .   . # @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ -     . % @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ # . : % @ @ @ @ @ @ @ @ @ @ @ @ @

访问像素数据现在应该比使用对象img.load()返回更快(并且值将是 0..255 范围内的整数)。

您可以通过访问 r、g 或 b 值来访问每个单独像素的灰度值,这些值对于灰度图像都是相同的。

img = Image.open('eggs.png').convert('1')
rawData = img.load()
data = []
for y in range(24):
    for x in range(24):
        data.append(rawData[x,y][0])

这并不能解决访问速度的问题。

我对 scikit-image 比 Pillow 更熟悉。 在我看来,如果您所追求的只是列出灰度值,您可以使用 scikit-image,它将图像存储为 numpy 数组,并使用 img_as_ubyte 将图像表示为 uint 数组,包含 0 到 255 之间的值。

图像是 NumPy 数组提供了一个很好的起点来查看代码的样子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM