0

I'm trying to convert an image into a matrix.

values = []
normal = []
for x in (arr):
    for y in (arr):
        if arr[x,y] > 1:
            normal.append(1)
        else:
            normal.append(0)

And error says:

"ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

Thank in advance.

Andrew
  • 26,706
  • 9
  • 85
  • 101
Xavier Johanis
  • 59
  • 2
  • 12

1 Answers1

0

It looks like arr is a 2d array. If you want to use x, y as index and arr is a 2d array then try this:

m, n = arr.shape
for x in range(m):
    for y in range(n):
        if arr[x,y] > 1:
            normal.append(1)
        else:
            normal.append(0)

However the code doesn't look pythonic. I would use numpy.where but it is hard to help without knowing what you really want to do.

hoang tran
  • 3,918
  • 3
  • 19
  • 21
  • 1
    thanks for the help, im really new to image processing in python so, can you give me an example where it converts the image into binary matrix? – Xavier Johanis Dec 01 '16 at 14:43
  • Take a look at these threads: http://stackoverflow.com/questions/23225738/convert-image-to-binary-stream and http://stackoverflow.com/questions/22351254/python-script-to-convert-image-into-byte-array Are you trying to do the same thing? – hoang tran Dec 01 '16 at 14:49
  • this is from your second comments, yes although I don't understand the first link – Xavier Johanis Dec 01 '16 at 14:56