RRSH
RRSH

Reputation: 1

AttributeError: 'str' object has no attribute 'shape' in python

I'm a beginner in python. I try to find out the Krawtchouk moments of the image. However, I get

AttributeError: str object has no attribute shape

import numpy as np
from krawtchouk import *

K = wkrchkpoly(101,0.5)

X = 'name.jpg'
Q,Kr1,Kr2 = wkrchkmoment_single(X,[0.5, 0.8])

Upvotes: 0

Views: 2910

Answers (1)

dspencer
dspencer

Reputation: 4461

Since wkrchkmoment_single takes a numpy.ndarray as its first argument (source code), you could use the PIL library to read your jpg, then convert it to a numpy.ndarray using np.asarray before passing it to wkrchkmoment_single.

from PIL import Image
import numpy as np

img = Image.open("name.jpg")
img.load()
X = np.asarray(img, dtype="int64")
Q,Kr1,Kr2 = wkrchkmoment_single(X,[0.5, 0.8])

Since wkrchkmoment_single expects a 2D np.ndarray as its first argument, corresponding to a grey scale image, you may need to convert your image from colour to grey scale. One way to do this would be by taking the mean across the third axis of the array, i.e.:

X = np.mean(np.array(img, dtype="float64"), axis=2)

Upvotes: 1

Related Questions