【问题标题】:print RGB values of image using wand in python在 python 中使用 wand 打印图像的 RGB 值
【发布时间】:2018-10-02 10:13:19
【问题描述】:

我已将图像大小调整为“1x1”,以便使用 python 中的 wand 库获得平均颜色,但现在我想打印调整大小的“1x1”图像的“RGB”值。我是新来的魔杖,所以任何指导或帮助将不胜感激。这是我到目前为止编写的代码。

with Image(filename='E:/Miro/images/test.jpg') as image:
with image.clone() as img:
    img.resize(1,1)

我只是想知道 wand 库中是否有一个函数可以访问图像的“RGB”值。

【问题讨论】:

    标签: python image-processing wand


    【解决方案1】:

    我不认为resize 1x1 是获取图像平均颜色的好方法。我不知道wand 是什么。但是如果你已经将图片读入numpy.ndarray,那么你可以得到这样的平均值:

    #!/usr/bin/python3
    # 2018.10.02 19:07:21 CST
    
    import cv2
    
    def getAvg(img):
        nh,nw = img.shape[:2]
        nc = 1 if img.ndim==2 else img.shape[2]
        avg = img.reshape(-1, nc).sum(axis=0)/(nw*nh)
        return avg
    
    img = cv2.imread("doll.jpg")
    avg = getAvg(img)
    print("Avg: {}".format(avg))
    
    # Avg: [148.661125 146.273425 155.205675]
    

    【讨论】:

    • 非常感谢,这真的很有帮助,但我认为 wand 的“resize 1x1”更快,只需调用一个函数即可完成,这就是我想使用它的原因@Silencer
    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多