Python的Image.load()返回超过250的像素
我正在用 Image.open("image.tif") 来加载我的图片文件。接着,我使用 Image.load() 来生成这张图片的像素图。然后,我把每个像素存储到一个数组里。下面的代码描述了这个过程。接下来,我想把每个像素的值转换成 ASCII 值,并把它们存储在一个字符串里。所以我遍历我的像素数组,把每个像素的值改成 ASCII 值。然而,我遇到了一个错误,因为我发现有些像素值超过了 250。这是怎么回事?而且这是一张黑白图片。我哪里做错了?
self.filename = filename
self.im = Image.open(filename)
self.pixelmap = self.im.load() #Loads the image as a map of pixels
self.arr = []
for i in range(self.im.size[0]):
for j in range(self.im.size[1]):
mypixel = self.pixelmap[i, j]
self.arr.append(mypixel)
for i in msgFile.arr:
self.message += str(unichr(int(i)))
1 个回答
1
像这样吗?
from PIL import Image
import numpy as np
image = np.asarray(Image.open('image.jpg'))
_y, _x, _z = image.shape
str_array = [str(image[y][x]) for y in range(_y) for x in range(_x)]