[SOLVED] Python: find white text in a black png image | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[SOLVED] Python: find white text in a black png image

I need to write a Python script for the recognition of the vertical position of a one-line white sentence in a black png image. The sentence is always the same but its size and position varies, as well the images’ size are different. The images contain other text lines but the line I need is always the top (first) line. As output I only need the text’s vertical coordinates. Any idea?

20th Feb 2020, 1:16 PM
Prof. Dr. Zoltán Vass
8 Answers
+ 2
Oma Falk Lothar As a side project, I have solved this problem in the following way: 1. Import image in numpy array 2. Sum all pixel’s red channel’s (could also be green or blue) value in row by row 3. If the sum is zero, the row is black with no letters, else it contains text 4. Find the first black row after the first letters’ row and save its y value 5. Crop the image using this value as top left point. Solved and tested, runs also in iOS: https://code.sololearn.com/cwmwSm9ui268/?ref=app Thanks Oma Falk for the inspiration :)
9th Mar 2020, 9:21 PM
Prof. Dr. Zoltán Vass
+ 1
Have a look at pillow
20th Feb 2020, 1:41 PM
Oma Falk
Oma Falk - avatar
+ 1
Oma Falk Yes, I need the first row with a white pixel.
20th Feb 2020, 1:48 PM
Prof. Dr. Zoltán Vass
+ 1
Oma Falk Pixel matrix? Do you mean searching for the last white pixel? The problem is how to find “the last” white pixel that belongs to the sentence: (1) There are black background pixels between white letters (in the first sentence). (2) Below the first line there are other white lines. (3) The vertical distance between the first sentence and the other lines are variable.
20th Feb 2020, 1:57 PM
Prof. Dr. Zoltán Vass
0
Oma Falk I did but Pillow is only about image manipulation
20th Feb 2020, 1:42 PM
Prof. Dr. Zoltán Vass
0
You need first row with a white pixel...right?
20th Feb 2020, 1:45 PM
Oma Falk
Oma Falk - avatar
0
So....that's it.
20th Feb 2020, 1:49 PM
Oma Falk
Oma Falk - avatar