如何将灰度图像转换为像素值列表?
How to convert a grayscale image into a list of pixel values?
我正在尝试创建一个 python 程序,它采用灰度、24*24 像素图像文件(我还没有决定类型,所以欢迎提出建议)并将其转换为列表从 0(白色)到 255(黑色)的像素值。
我计划使用这个数组来创建一个类似 MNIST 的图片字节文件,它可以被 Tensor-Flow 手写识别算法识别。
我发现 Pillow library 在此任务中最有用,它遍历每个像素并将其值附加到数组
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])
然而这个解决方案有两个问题:
- 像素值不是存储为整数,而是不能进一步进行数学操作的像素对象,因此是无用的。
- 甚至 Pillow docs 也指出:
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.
您可以通过访问 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。
Images are NumPy Arrays 提供了一个很好的起点来查看代码的样子。
您可以像这样将图像数据转换成 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
小图像的放大版本:
这是第一个访问示例的输出:
这里是第二个示例的输出:
@ @ % * @ @ @ @ % - . * @ @ @ @ @ @ @ @ @ @ @ @
@ @ . . + @ # . = @ @ @ @ @ @ @ @ @ @ @ @
@ * . . * @ @ @ @ @ @ @ @ @ @ @ @
@ # . . . . + % % @ @ @ @ # = @ @ @ @
@ % . : - - - : % @ % : # @ @ @
@ # . = = - - - = - . . = = % @ @ @
@ = - = : - - : - = . . . : . % @ @ @
% . = - - - - : - = . . - = = = - @ @ @
= . - = - : : = + - : . - = - : - = : * %
- . . - = + = - . . - = : - - - = . -
= . : : . - - . : = - - - - - = . . %
% : : . . : - - . : = - - - : = : # @
@ # : . . = = - - = . = + - - = - . . @ @
@ @ # . - = : - : = - . - = = : . . # @
@ @ % : = - - - : = - : - . . . - @
@ @ * : = : - - - = . . - . . . + @
@ # . = - : - = : : : . - % @ @ @
* . . . : = = - : . . - . - @ @ @ @ @
* . . . : . . . - = . = @ @ @ @ @ @
@ : - - . . . . # @ @ @ @ @ @ @ @
@ @ = # @ @ * . . . - @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ . . . # @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ - . % @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ # . : % @ @ @ @ @ @ @ @ @ @ @ @ @
访问像素数据现在应该比使用对象更快 img.load()
returns(并且值将是 0..255 范围内的整数)。
我正在尝试创建一个 python 程序,它采用灰度、24*24 像素图像文件(我还没有决定类型,所以欢迎提出建议)并将其转换为列表从 0(白色)到 255(黑色)的像素值。
我计划使用这个数组来创建一个类似 MNIST 的图片字节文件,它可以被 Tensor-Flow 手写识别算法识别。
我发现 Pillow library 在此任务中最有用,它遍历每个像素并将其值附加到数组
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])
然而这个解决方案有两个问题:
- 像素值不是存储为整数,而是不能进一步进行数学操作的像素对象,因此是无用的。
- 甚至 Pillow docs 也指出:
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.
您可以通过访问 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。
Images are NumPy Arrays 提供了一个很好的起点来查看代码的样子。
您可以像这样将图像数据转换成 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
小图像的放大版本:
这是第一个访问示例的输出:
这里是第二个示例的输出:
@ @ % * @ @ @ @ % - . * @ @ @ @ @ @ @ @ @ @ @ @
@ @ . . + @ # . = @ @ @ @ @ @ @ @ @ @ @ @
@ * . . * @ @ @ @ @ @ @ @ @ @ @ @
@ # . . . . + % % @ @ @ @ # = @ @ @ @
@ % . : - - - : % @ % : # @ @ @
@ # . = = - - - = - . . = = % @ @ @
@ = - = : - - : - = . . . : . % @ @ @
% . = - - - - : - = . . - = = = - @ @ @
= . - = - : : = + - : . - = - : - = : * %
- . . - = + = - . . - = : - - - = . -
= . : : . - - . : = - - - - - = . . %
% : : . . : - - . : = - - - : = : # @
@ # : . . = = - - = . = + - - = - . . @ @
@ @ # . - = : - : = - . - = = : . . # @
@ @ % : = - - - : = - : - . . . - @
@ @ * : = : - - - = . . - . . . + @
@ # . = - : - = : : : . - % @ @ @
* . . . : = = - : . . - . - @ @ @ @ @
* . . . : . . . - = . = @ @ @ @ @ @
@ : - - . . . . # @ @ @ @ @ @ @ @
@ @ = # @ @ * . . . - @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ . . . # @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ - . % @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ # . : % @ @ @ @ @ @ @ @ @ @ @ @ @
访问像素数据现在应该比使用对象更快 img.load()
returns(并且值将是 0..255 范围内的整数)。