1

I'm trying to compute the gradient of an image for sharpness check but the following code throws an error even though it should be pretty straight forward

import os,sys
from PIL import Image
import numpy as np

B = Image.open("C:\\work\\tech\\blur\\blur.jpg")
By, Bx = np.gradient(B)
Bnorm = np.sqrt(Bx**2 + By**2)
Bsharpness = np.average(Bnorm)

print Bsharpness

Error

----> 6 By, Bx = np.gradient(B)
ValueError: too many values to unpack 

The documentation for np.gradient says

For two dimensional arrays, the return will be two arrays ordered by axis.

I have checked this question and it doesn't answer mine

Community
  • 1
  • 1
Hungi Mahar
  • 89
  • 1
  • 6
  • Possible duplicate of ['too many values to unpack', iterating over a dict. key=>string, value=>list](http://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list) – cagatayodabasi Nov 03 '16 at 15:16
  • Try `print(np.gradient(B))` and see what it is you're actually getting back – Patrick Haugh Nov 03 '16 at 15:21
  • @cagatayodabasi I have read that topic first and afaik the sollution does not apply here. – Hungi Mahar Nov 03 '16 at 15:21
  • 5
    your image `B` is not a "two dimensional arrays" as it contains the 3 channels RGB, therefore your gradient is a 3d gradient (direction in x axis, direction in y axis, direction in channel) – JMat Nov 03 '16 at 15:27

0 Answers0