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
#3
What did you do to understand this problem before posting? Did you look at the shape of numpy_array? Did you read enough about numpy to know that they can be multidimensional?
 
numpy_arrray (a terrible variable name) will be a 3-dimensional array. You will know this if you look at the shape. The first dimension are rows in the image. The second dimension are pixels in the rows, and the third dimension are red, green, blue (, opacity) values for each pixel. In your code, i[0] is the first pixel in a row, not red. [i][1] and [i][2] are not python.

You could create an array of pixels by reshaping the array.
pixels = np.array(Image.open("p2.bmp"))
rows, columns, rgba = pixels.shape
print(rows, columns, rgba
pixels = np.reshape(pixels, (-1, rgba))
Now you have an array of pixels, not an image. You can do this:
for pixel in pixels:
   r, g, b = pixel
Now you learn your logic is wrong. RGB for cyan is (0, 255, 255). RGB for magenta is (255, 0, 255) RGB for yellow is (255, 255, 0). According to your program logic, cyan, magenta and yellow are all white. According to your logic, red, green and blue are also white. All of these colors have one or more components that == 255.
for pixel in pixels:
   r, g, b = pixel
   if r < 255 or g < 255 or b < 255:
       print(pixel)l
The description of your problem is unclear. Do you want a count of pixels, or is a single non-white pixel enough to satisfy the test? Do you care about the color of this pixel, or is it only important that it not be white?

If all you want to know is if there is any non-white pixel in the image, I would forget about pixel values and test the individual components.
import numpy as np
from PIL import Image

values = np.array(Image.open("test.png"))[:, :, :3].flatten()
print(np.any(values < 255))
Or
import numpy as np
from PIL import Image

image = Image.open("test.png")
pixels = np.array(image)
rgb_pixels = pixels[:, :, :3]  # Throw away any opacity information.
values = rgb_pixels.flatten()  # Make a 1-dimensional array
values_less_than_255 = values < 255  # array of bool.  True if value < 255
any_value_less_than_255 = np.any(values_less_than_255)  # Are any values < 255?
Bitmaps don't have opacity information. If you are only ever going to test a bitmap file you can skip the slicing step.
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 deanhystad - Sep-24-2023, 12:53 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 122 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,103 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  Need to identify only files created today. tester_V 5 4,833 Feb-18-2021, 06:32 AM
Last Post: tester_V
  pillow reversing the order of pixels after every row johnEmScott 4 3,234 May-27-2020, 09:42 AM
Last Post: scidam
  Need to identify sheet color in excel workbook chewy1418 2 2,608 Feb-14-2020, 03:26 PM
Last Post: chewy1418
  Convert 400 grayscale pixels into RGB python420 1 2,526 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,007 Dec-03-2019, 11:05 PM
Last Post: Pleiades
  White spaces kdiba 1 2,035 Oct-08-2019, 06:52 PM
Last Post: Aurthor_King_of_the_Brittons
  including the white space parts in str.split() Skaperen 6 3,411 Jun-20-2019, 06:03 PM
Last Post: Skaperen
  replace white space with a string, is this pythonic? Skaperen 1 2,072 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