python - 在 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”值。
解决方案
我认为这不是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]
推荐阅读
- java - 如何返回泛型类型的类
- reactjs - 如何在 nextjs 中使用 react-bodymovin
- javascript - Bootstrap 4 JS 菜单选项卡
- java - 在 Eclipse 中启动一个简单的 Maven 项目的问题?
- ruby-on-rails - Rails 查询 has_many 与 Geocoder 的关联
- html - HTML 输入占位符文本溢出:聚焦时省略号不起作用(仅限 webkit)
- python - 当我从值列表中搜索任何特定值时,从字典中提取键
- java - 猜谜游戏0-100 java
- java - 如何从 aysnc 回调创建多个 Flux
- vue.js - Vuetify 对话框不会在数据表中打开