PIL获取图片亮度值的五种方式

import math
from PIL import Image, ImageStat


def get_image_light_mean(dst_src):
    im = Image.open(dst_src).convert('L')
    stat = ImageStat.Stat(im)
    return stat.mean[0]


def get_image_light_rms(dst_src):
    im = Image.open(dst_src).convert('L')
    stat = ImageStat.Stat(im)
    return stat.rms[0]


def get_image_light_mean_sqrt(dst_src):
    im = Image.open(dst_src)
    stat = ImageStat.Stat(im)
    r, g, b = stat.mean
    return math.sqrt(0.241 * (r ** 2) + 0.691 * (g ** 2) + 0.068 * (b ** 2))


def get_image_light_rms_sqrt(dst_src):
    im = Image.open(dst_src)
    stat = ImageStat.Stat(im)
    r, g, b = stat.rms
    return math.sqrt(0.241 * (r ** 2) + 0.691 * (g ** 2) + 0.068 * (b ** 2))


def get_image_light_gs(dst_src):
    im = Image.open(dst_src)
    stat = ImageStat.Stat(im)
    gs = (math.sqrt(0.241 * (r ** 2) + 0.691 * (g ** 2) + 0.068 * (b ** 2))
          for r, g, b in im.getdata())
    return sum(gs) / stat.count[0]
原文地址:https://www.cnblogs.com/shinelover/p/10797371.html