简体   繁体   中英

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])

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, ie:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM