首页 > 解决方案 > 在 python 中使用 wand 打印图像的 RGB 值

问题描述

我已经将图像大小调整为“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”值。

标签: pythonimage-processingwand

解决方案


我认为这不是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]

推荐阅读