0

我正在尝试制作一个程序,该程序通过模拟一行文本并从中获取每个字母的图像。考虑二维像素阵列的图像,如果连续列中存在黑色像素,则这些列将被写入缓冲区。一旦到达没有黑色像素的列(即字母之间的空间),缓冲区将变成已检测到的字母的图像。但是,我遇到了一个我不明白的编译器错误,希望你们能帮助我。希望您也能发现我没有发现的任何逻辑错误。

无论如何,代码:

from PIL import Image
import numpy as np

class ShapeDetect:
    def __init__(self):
        self.img = Image.open("test3.jpg")
        self.pixels = self.img.load()
        self.l = np.empty([0, 0])
        self.valid_pixels = ['(0, 0, 0)', '(1, 1, 1)', '(2, 2, 2)', '(3, 3, 3)', '(4, 4, 4)', '(5, 5, 5)']

def printPixels(self):
    for i in range(self.img.size[0]):
        for j in range(self.img.size[1]):
            print(self.pixels[i, j])

def processPixels(self):
    n = 1

    # If a black pixel is in the current column, add it to l
    for i in range (self.img.size[0]):
        for j in range(self.img.size[1]):
            if str(self.pixels[i, j]) in self.valid_pixels:
                self.writeColumn(i)
                break

            # Once a whole shape has been written to l, make l into an image
            else:
                 if self.l.size > 0 and j == self.img.size[1] - 1:
                    new_img = Image.new(self.img.mode, (self.l.size, 100))
                    new_img.putdata(self.l)
                    new_img.save(str(n) + ".jpg")

                    n = n + 1
                    self.l = np.empty([0], [0])

def writeColumn(self, n):
    # put column in pixels in temp, then append temp to l
    temp = np.zeros((1, self.img.size[1]), dtype=np.int64)
    for i in range(self.img.size[1]):
        temp[0, i], = (self.pixels[n, i])

    np.append(self.l, temp, axis = 1)

if __name__ == "__main__":
    shapeDetect = ShapeDetect()
    ShapeDetect.processPixels(shapeDetect)

我得到的错误是:

Traceback (most recent call last):
  File "SD.py", line 46, in <module>
    ShapeDetect.processPixels(shapeDetect)
  File "SD.py", line 23, in processPixels
    self.writeColumn(i)
  File "SD.py", line 40, in writeColumn
    temp[0, i], = (self.pixels[n, i])
ValueError: too many values to unpack
4

1 回答 1

0

发生错误是因为self.pixels[n, i]返回一个像素,该像素具有 3 个值。看起来您实际上想要所有 3 个值,但您错误地在temp[0,i]. 删除逗号将解决问题。

但是,有一种更快的方法来提取列。你可以更换

temp = np.zeros((1, self.img.size[1]), dtype=np.int64)
for i in range(self.img.size[1]):
    temp[0, i], = (self.pixels[n, i])

temp = self.pixels[n, :]

对于 numpy 数组

于 2014-06-13T00:17:37.183 回答