Search code examples
pythonnumpyscipyimage-classificationpython-imageio

too many values to unpack (while reading image's shape)


I am trying to run this code snippet:

from scipy.stats import wasserstein_distance
from imageio import imread
import numpy as np

def get_histogram(img):
  h, w = img.shape
  hist = [0.0] * 256
  for i in range(h):
    for j in range(w):
      hist[img[i, j]] += 1
  return np.array(hist) / (h * w)

a = imread("./IMG_4835.jpg")
b = imread("./IMG_4836.jpg")
a_hist = get_histogram(a)
b_hist = get_histogram(b)
dist = wasserstein_distance(a_hist, b_hist)
print(dist)

but I get an error at:

h, w = img.shape
b = imread('b.jpg', mode='L')
ValueError: too many values to unpack (expected 2)

The original code used:

from scipy.ndimage import imread

to read the image file but since I was unable to import it, I used imread from another library instead. Could that have anything to do with the error?


Solution

  • h,w = img.shape[:2] should fix the problem.