0

i have resized an image into "1x1" in order to get the average color using wand library in python but now i want to print the "RGB" values of that resized "1x1" image. i am new to wand so any guidance or help would be really appreciated.this is the code i have written so far.

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

i just want to know if there is a function in wand library to access "RGB" values of image.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Tehseen
  • 115
  • 2
  • 14

1 Answers1

2

I don't think resize 1x1 is a good method to get the average color of the image. I don't know what wand is. But if you have read the image into numpy.ndarray, then you can get the avg like this:

#!/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]
Kinght 金
  • 17,681
  • 4
  • 60
  • 74
  • thanks much, that's really helpful but i think wand's "resize 1x1" is faster and it can be done by calling just one function, that's why i want to use it @Silencer – Tehseen Oct 03 '18 at 06:45