Psionman
Psionman

Reputation: 3699

How to change pixel colours in an image using numpy

I have an image that is converted to a numpy array

np_image = np.array(Image.open(filename))

I am attempting OCR using pytesseract, but the ocr fails when the text is red or yellow and so I want all text to be black.

I am breaking down the image into snippets as I know where the individual text elements appear

As you can see from the code below, I have attempted to find coloured pixels and and convert them to black, but it does not work

snippet = np_image[top: bottom, field.left: field.right].copy()
for row in snippet:
    for pixel in row:
        test = [rgb for rgb in pixel[:-1]]
        test.sort()
        if test[0] > 190 and test[2] < 100:
            pixel = [0, 0, 0, 255]

text = pytesseract.image_to_string(snippet)

What should I do?

Upvotes: 0

Views: 501

Answers (1)

J&#233;r&#244;me Richard
J&#233;r&#244;me Richard

Reputation: 50846

pixel = [0, 0, 0, 255] does not write in the actual image. It just set the value [0, 0, 0, 255] to the local variable pixel. Assuming np_image is a Numpy array, you certainly need to write pixel[:] = [0, 0, 0, 255] so that np_image can be modified.

Moreover, the sort function sorts the values in an increasing order. Thus, the condition seems suspiciously wrong. Indeed, test[0] <= test[2] must always be true. Thus, if test[0] > 190 is true, then test[2] < 100 cannot be true. As a result, the condition should never be true.

Upvotes: 1

Related Questions