【问题标题】:calculate average color baseline of image using wand in python在python中使用魔杖计算图像的平均颜色基线
【发布时间】:2018-10-01 09:17:11
【问题描述】:

我想在 python 中使用 wand 计算图像的平均颜色。我已经尝试过可以做到这一点的代码,但该代码位于“imagemagick”包中,我在安装时遇到了一些问题。这里的任何人都可以帮助我吗..我非常感谢任何指导帮助。这是“imagemagick”包的代码。我对魔杖库真的很陌生。

convert <input.jpg> -resize 1x1\! \
-format "%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]" info:-

【问题讨论】:

标签: python image-processing data-science wand


【解决方案1】:

有了,你已经拥有了python应用简单数学的能力。

with Image(filename='rose:') as img:
    img.resize(1,1)
    with img[0, 0] as color:
        r = int(255.0 * color.red + 0.5)
        g = int(255.0 * color.green + 0.5)
        b = int(255.0 * color.blue + 0.5)
    print('{0},{1},{2}'.format(r,g,b))

但这可以进一步简化...

with Image(filename='rose:') as img:
    img.resize(1,1)
    with img[0, 0] as color:
        r = math.ceil(255 * color.red)
        g = math.ceil(255 * color.green)
        b = math.ceil(255 * color.blue)
    print('{0},{1},{2}'.format(r,g,b))

或者更进一步……

with Image(filename='rose:') as img:
    img.resize(1,1)
    with img[0, 0] as color:
        r = color.red_int8
        g = color.green_int8
        b = color.blue_int8
    print('{0},{1},{2}'.format(r,g,b))

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2018-04-08
    • 2017-04-03
    • 2014-03-01
    • 2012-07-28
    • 2017-02-27
    相关资源
    最近更新 更多