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