我已将图像大小调整为“ 1x1”,以便使用python中的魔杖库获得平均颜色,但现在我想打印该大小调整后的“ 1x1”图像的“ RGB”值。我是魔杖的新手,所以对您的指导或帮助将不胜感激。这是我到目前为止编写的代码。
with Image(filename='E:/Miro/images/test.jpg') as image:
with image.clone() as img:
img.resize(1,1)
我只是想知道魔杖库中是否有一个函数可以访问图像的“ RGB”值。
答案 0 :(得分:2)
我认为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]