0

I'm trying to make a program that goes through an image that simulates a line of text and grabs each letter from it. Thinking of the image of a 2D array of pixels, if there exist black pixels in consecutive columns, those columns will be written to a buffer. Once a column with no black pixels has been reached (i.e. space between letters) the buffer will be turned in to an image of the letter that has been detected. However, I'm getting a compiler error that I don't understand that I hope you guys can help me with. Hopefully you'll also catch on to any logic errors I haven't recognized.

Anyway, code:

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)

The error I get is:

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
Stumbleine75
  • 391
  • 2
  • 7
  • 22

1 Answers1

0

The error happened because self.pixels[n, i] returns a pixel, which have 3 values. Looks like you have actually want all 3 values, but you had mistakenly placed a comma after temp[0,i]. Removing the comma would fix the issue.

However, there is a quicker way to extract the column. You can replace

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

with

temp = self.pixels[n, :]

for numpy arrays

Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • temp = self.pixels[n, :] caused TypeError: an integer is required. Declaring temp as a numpy array didn't fix it. – Stumbleine75 Jun 13 '14 at 02:11
  • @ThroatOfWinter57, I thought `self.pixels` was a numpy array. You can either change it to a numpy array, or use your original method. – Fabricator Jun 13 '14 at 02:18