+ 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?
8 ответов
+ 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 :)
+ 1
Have a look at pillow
+ 1
Oma Falk Yes, I need the first row with a white pixel.
+ 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.
0
Oma Falk I did but Pillow is only about image manipulation
0
You need first row with a white pixel...right?
0
So....that's it.