|
发表于 2022-3-25 16:05:57
|
显示全部楼层
本楼为最佳答案
- from itertools import product
- from PIL import Image
- def main(fn):
- img = Image.open(fn)
- pixels = img.load()
- total_pixels = img.width * img.height
- above_threshold_pixels = 0
- if img.mode == 'RGB':
- for x, y in product(range(img.width), range(img.height)):
- r, g, b = pixels[x, y]
- avg_color = (r + g + b) / 3
- if avg_color >= 200:
- above_threshold_pixels += 1
- elif img.mode == 'L':
- for x, y in product(range(img.width), range(img.height)):
- gray = pixels[x, y]
- if gray >= 200:
- above_threshold_pixels += 1
-
- ratio = round(above_threshold_pixels / total_pixels, 2)
- return ratio
复制代码
 |
|