I'm working on a script that is going to analize an image, I have a test image and the output image, the dimensions are 960x540 but I want to use this program on 4K images.
Basically my program is going to open the test image and convert it in a numpy array (as numpy.uint8) and using some for loops iterate over every single pixel and get the RGB values, sum them and if the sum is greater or equal then 765 ( (255+255+255)/2 ) make that pixel all white, and if the sum is less than 765 make that pixel black.
The script that I wrote is the following:
from PIL import Image
import numpy
img = Image.open('test.bmp')
img = numpy.array(img, numpy.uint8)
for il, line in enumerate(img):
for ip, pixels in enumerate(line):
if int(pixels[0]) + int(pixels[1]) + int(pixels[2]) >= 765:
img[il][ip] = 255, 255, 255
else:
img[il][ip] = 0, 0, 0
img = Image.fromarray(img.astype('uint8'))
img.save('test_new.bmp')
I know that other peoples alredy responded to this in their threads but I want to understand why my script failed and generated that wrong output image (see links above).
Thanks
EDIT:
Thanks to an user I found out why my script wan't working perfectly, I still don't know why, but i included the results.
Result compared