Mohit Yadav
Mohit Yadav

Reputation: 45

TypeError: slice indices must be integers or None or have an __index__ method. How to resolve it?

 if w<h:
        normalized_char = np.ones((h, h), dtype='uint8')
        start = (h-w)/2
        normalized_char[:, start:start+w] = char
    else:
        normalized_char = np.ones((w, w), dtype='uint8')
        start = (w-h)/2
        normalized_char[start:start+h, :] = char

Running this on jupyter and getting this error

<ipython-input-8-15d17de04b9c> in extractCharactersNew(function)
     60             normalized_char = np.ones((h, h), dtype='uint8')
     61             start = (h-w)/2
---> 62             normalized_char[:, start:start+w] = char
     63         else:
     64             normalized_char = np.ones((w, w), dtype='uint8')

TypeError: slice indices must be integers or None or have an __index__ method

How Can I resolve this error?

Upvotes: 0

Views: 9473

Answers (1)

Abhijeetk431
Abhijeetk431

Reputation: 846

Your error is:-

TypeError: slice indices must be integers or None or have an __index__ method

slice indices, in your case refer to the variables that you are using to slice the list in

normalized_char[:, start:start+w] = char

That is - start and start+w. For list slicing, these must be integers or have an __index__ method. This __index__ method is a special method that returns integer value for that object. You should be able to solve your issue by ensuring that you provide correct slice indices. You can use start = (h-w)//2(integer division) to make sure that start is an integer.

Upvotes: 2

Related Questions