Feb-06-2024, 08:36 PM
Hi,
thanks for your answers.
And thanks, Pedroski55, for your code example!
I would like to have a more friendly tone - I'm not so experienced and I'm trying to get better...
I thought examining pixels would work for both (text and pictures).
You both are more experienced than I am.
I don't know if a solution, which is based on text recognition, is reliable.
And I don't know how to detect pictures.
I thought therefore "messing around with pixels" would be a good idea.
That's because I'm not so experienced...
I will try Pedroski55's example too, just to get more experience and to compare both things.
Even though I did the effort for free, it was good for gaining more programming experience.
Please let me know what you think of my code (if you are still interested):
Here is what I did in the meantime:
The function getcolors is limited to 256 colors, otherwise None is returned.
Therefore a scan can contain more than 256 colors, I quantize the image of the scan to 2 colors.
Then I use the function getcolors to count the amount of pixels (the one color is the "filled" content and the other is the "background" content).
I tested it creating a 10 x 10 pixel bmp with various amounts of "filled" pixels.
All counted colors were correct.
I will test the speed of counting pixels tomorrow using a DIN A4 - sheet.
thanks for your answers.
And thanks, Pedroski55, for your code example!
I would like to have a more friendly tone - I'm not so experienced and I'm trying to get better...
I thought examining pixels would work for both (text and pictures).
You both are more experienced than I am.
I don't know if a solution, which is based on text recognition, is reliable.
And I don't know how to detect pictures.
I thought therefore "messing around with pixels" would be a good idea.
That's because I'm not so experienced...
I will try Pedroski55's example too, just to get more experience and to compare both things.
Even though I did the effort for free, it was good for gaining more programming experience.
Please let me know what you think of my code (if you are still interested):
Here is what I did in the meantime:
The function getcolors is limited to 256 colors, otherwise None is returned.
Therefore a scan can contain more than 256 colors, I quantize the image of the scan to 2 colors.
Then I use the function getcolors to count the amount of pixels (the one color is the "filled" content and the other is the "background" content).
I tested it creating a 10 x 10 pixel bmp with various amounts of "filled" pixels.
All counted colors were correct.
I will test the speed of counting pixels tomorrow using a DIN A4 - sheet.
from PIL import Image def count_colors_2(image, maxcolors=2): # reducing colors image_reduced = image.quantize(colors=maxcolors, method=None, kmeans=0, palette=None) colors = image_reduced.getcolors(maxcolors) count_background_pixel = colors[0][0] count_filled_pixel = colors[1][0] return count_background_pixel, count_filled_pixel, colors image = Image.open("25.bmp") count_background_pixel, count_filled_pixel, colors = count_colors_2(image) print("count_background_pixel: " + str(count_background_pixel)) print("count_filled_pixel: " + str(count_filled_pixel)) print(colors)