如何将灰度图像转换为像素值列表? [英] How to convert a grayscale image into a list of pixel values?

查看:195
本文介绍了如何将灰度图像转换为像素值列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a python program which takes a grayscale, 24*24 pixel image file (I haven't decided on the type, so suggestions are welcome) and converts it to a list of pixel values from 0 (white) to 255 (black).

我计划使用此数组来创建图片的 MNIST 类似的字节文件,可以通过Tensor-Flow手写识别算法进行识别.

I plan on using this array for creating a MNIST-like bytefile of the picture, that can be recognized by Tensor-Flow handwriting recognition algorithms.

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

I have found the Pillow library to be the most useful in this task, by iterating over each pixel and appending its value to an array

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])

但是此解决方案有两个问题:

Yet this solution has two problems:

  1. 像素值未存储为整数,但无法进一步进行数学处理,因此无用的像素对象.
  2. 即使枕头文档指出:

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

Accessing individual pixels is fairly slow. If you are looping over all of the pixels in an image, there is likely a faster way using other parts of the Pillow API.

推荐答案

您可以像这样将图像数据转换为Python列表(或列表列表):

You can convert the image data into a Python list (or list-of-lists) like this:

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 图片测试:

Here's an enlarged version of a small 24x24 RGB eggs.png image I used for testing:

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

Here's the output from the first example of access:

这里是第二个示例的输出:

And here the output from the second example:

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

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

Access to the pixel data should now be faster than using the object img.load() returns (and the values will be integers in the range of 0..255).

这篇关于如何将灰度图像转换为像素值列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆