Python Forum
identify not white pixels in bmp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
identify not white pixels in bmp
#7
Dear deanhystad,
I'm trying to count the pixels with the same RGB values. In a previous post I tried to determine when an image (and therefore a PDF) is empty. But it was limited to the background color white. I thought of the following: You determine the sums of the pixels of the same color. If there is only 1 sum, then the picture is empty. If there are 2 or more pixel sums with the same color: ignore the largest sum and consider all the next smallest sums. The proportion of the next smallest sums in the overall image is the proportion of the overall image that is filled. You should be able to specify a proportion from which the image is considered filled.

import numpy as np
import pandas as pd
from PIL import Image

image = Image.open("p2.bmp")
pixels = np.array(Image.open("p2.bmp").convert('RGB'))
rows, columns, rgba = pixels.shape
pixels = np.reshape(pixels, (-1, rgba))
#mergeArray = []
# for pixel in pixels:
#    r, g, b = pixel
#    a = str(r) + str(g) + str(b)
#    mergeArray.append(a)
# count_same_RGB = np.unique(mergeArray)
arr_colors, arr_counts = np.unique(pixels.reshape(-1, 3), axis=0, return_counts=1)
print(arr_colors)
print(arr_counts)

# is image empty? (largest_amount_pixels_same_RGB == amount_pixels_image)
amount_pixels_whole_image = image.width * image.height
largest_amount_pixels_same_RGB = np.max(arr_counts)
# # remove largest amount pixels same RGB from arr_counts
arr_counts_2 = np.delete(arr_counts, np.where(arr_counts == largest_amount_pixels_same_RGB))

sum_portions_filled_pixels = sum(arr_counts_2)/amount_pixels_whole_image
print("sum_portions_filled_pixels = " + str(sum_portions_filled_pixels))
#
userdef_image_is_full = 0.1
if len(arr_colors) == 1 or sum_portions_filled_pixels < userdef_image_is_full:
    print("The image is empty")
else:
    print("The image is filled")
What do you think of my idea?

My test image is only 10 x 10 pixel:
I'm planning to test scanned pages of paper (will be much bigger than 10 x 10 pixel).
Would it be better to use pandas for a real page(it seems to be much faster than numpy)?

Thanks a lot for your outstanding, detailed help!!
Reply


Messages In This Thread
identify not white pixels in bmp - by flash77 - Sep-24-2023, 10:07 AM
RE: identify not white pixels in bmp - by DPaul - Sep-24-2023, 12:50 PM
RE: identify not white pixels in bmp - by flash77 - Sep-24-2023, 02:28 PM
RE: identify not white pixels in bmp - by ALIII - Sep-24-2023, 02:51 PM
RE: identify not white pixels in bmp - by flash77 - Oct-04-2023, 04:47 AM
RE: identify not white pixels in bmp - by flash77 - Oct-05-2023, 05:16 PM
RE: identify not white pixels in bmp - by flash77 - Oct-05-2023, 07:52 PM
RE: identify not white pixels in bmp - by flash77 - Oct-07-2023, 04:58 PM
RE: identify not white pixels in bmp - by flash77 - Oct-07-2023, 07:48 PM
RE: identify not white pixels in bmp - by flash77 - Oct-08-2023, 09:21 PM
RE: identify not white pixels in bmp - by flash77 - Oct-09-2023, 06:18 AM
RE: identify not white pixels in bmp - by flash77 - Nov-10-2023, 09:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help to identify CNA for automation qatester 0 173 May-31-2024, 09:24 AM
Last Post: qatester
  guys please help me , pycharm is not able to identify my xlsx file CrazyGreenYT7 1 2,133 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  Need to identify only files created today. tester_V 5 4,910 Feb-18-2021, 06:32 AM
Last Post: tester_V
  pillow reversing the order of pixels after every row johnEmScott 4 3,291 May-27-2020, 09:42 AM
Last Post: scidam
  Need to identify sheet color in excel workbook chewy1418 2 2,653 Feb-14-2020, 03:26 PM
Last Post: chewy1418
  Convert 400 grayscale pixels into RGB python420 1 2,552 Jan-02-2020, 04:19 PM
Last Post: Clunk_Head
  Need help to identify Mersenne Primes, I do need a search pattern. Pleiades 0 2,049 Dec-03-2019, 11:05 PM
Last Post: Pleiades
  White spaces kdiba 1 2,054 Oct-08-2019, 06:52 PM
Last Post: Aurthor_King_of_the_Brittons
  including the white space parts in str.split() Skaperen 6 3,477 Jun-20-2019, 06:03 PM
Last Post: Skaperen
  replace white space with a string, is this pythonic? Skaperen 1 2,097 Jun-18-2019, 11:36 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020