简体   繁体   中英

Python/Pillow: Filter pixels with max/min RGB values

I have an image in which every pixel is a random combination of maximum and minimum RGB values (ie [255, 0, 255], [0, 0, 255], [255,255,255]).

However, the image does contain some text that is hidden as a very slight, imperceptible discoloration in the noise. I am trying to write some simple Python to whiten all pixels containing maximum or minimum RGB values so that the hidden message will be clearly visible.

The program should be extremely simple, something like this:

open image
get width and height
for x in width
   for y in height
       if any channel value of pixel (x,y) is either 0 or 255
          set color of (x,y) to [255, 255, 255]

The main part I'm stuck on is the condition for changing the color; I could separately check first the red value of each pixel, then green, then blue, but since I am trying to make the code as compact as possible, I would like to know how to test all three elements in the array simultaneously rather than using separate if statements. Any hints would be appreciated. Thanks!

Since the RGB values are given as a single array, you can use the keyword in :

# channels = [255, 0, 0]
if 0 in channels or 255 in channels:
    channels = [255, 255, 255]

You can even combine multiple in checks , but that would be too much for just 2 checks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM