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

4

我正在尝试创建一个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 docs也指出:
  3. 访问单个像素相当缓慢。如果您正在循环遍历图像中的所有像素,则可能使用Pillow API的其他部分有更快的方法。


当我在我的机器上运行你的代码时,data 是一个常规整数列表。 - Kevin
2
文档可能是指getdata,我预计它比逐像素访问更快。 - Kevin
我不知道有任何Python库可以在单个像素访问时不会变慢。 - Douglas
2个回答

17

您可以像这样将图像数据转换为 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范围内的整数)。


0

您可以通过访问 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数组提供了一个很好的起点,可以看到代码的样子。


1
关于访问图像中单个像素的说法,在将图像转换为模式'1'后不适用,您当前的代码将导致最后一行中的rawData[x,y][0]出现TypeError: 'int' object is not subscriptablerawData中的值都是单个整数值,要么是0,要么是255 - martineau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接