2

I'm trying to convert a grayscale image's pixels into a numpy array.

Working on google colab.

it shows an error saying: TypeError: 'numpy.uint8' object is not iterable

enter code here

    ##load Library
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    from google.colab import files
    from scipy import misc #to see image
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LogisticRegression

    from PIL import Image
    pil_im = Image.open('papa.png')
    pil_imgray = pil_im.convert('LA')

    img = np.array(list(pil_imgray.getdata(band=0)), float)
    img.shape = (pil_imgray.size[1], pil_imgray.size[0])
    plt.imshow(img)

    for eachRow in img:
      for eachPixel in eachRow:
          x_test.append(sum(eachPixel)/3.0)  
2
  • I which line does your error occur? Commented Feb 2, 2019 at 7:17
  • Last line when i try to iterate x_test Commented Feb 3, 2019 at 8:23

1 Answer 1

1

You can directly load the image using matplotlib:

plt.imread('papa.png')

or you can convert your PIL image with:

img = np.asarray(pil_im)
Sign up to request clarification or add additional context in comments.

2 Comments

it shows an error saying: NameError: name 'pil_im' is not defined
well, pil_im has to be defined. But you had the line pil_im = Image.open('papa.png') loading the image and storing it in the variable pil_im.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.