black-percentage-bot / bpb

A Python3 based Reddit bot calculating the true black pixel percentage of submissions, with comment support and an offline feature.
Do What The F*ck You Want To Public License
28 stars 2 forks source link

Performance Suggestion #6

Open filo-paa opened 3 years ago

filo-paa commented 3 years ago

replace the code that computes the ratio with this much faster:

from PIL import Image import numpy as np

imname = 'test.png' myGreyScaledImage = Image.open(imname).convert('L') im_array = np.array(myGreyScaledImage) black_ratio = np.count_nonzero(im_array==0)/im_array.size

print(black_ratio)

feel free to test it

Cheers