0

i am trying to recreate a picture. I take a picture edging it and save it. after i made it grayscale and save it. Found the common pixels of the two images and I am trying to recreate again the picture and i get this error. It is a picture of a road and i am trying to keep only the white lanes. so after i compare the edged picture with the first picture most common pixels are the white ones that represent the lanes of the road.

The error is thrown in line marked <———-- near the end of the code listing

TypeError: too many data entries 

newpic is the list in that form `[1,1,1,1,...,1]

here is my code and explaining every part. if you have any other suggestion how to achieve the result i want please say it

    #LIBRARIES
    import cv2
    import numpy as np 
     import matplotlib as mpl
    from matplotlib import pyplot as plt

    #read and display the image
    img = cv2.imread("road.jpg")

    #original picture show
    cv2.imshow("Window Name",img)

    # edging the image
    edges = cv2.Canny(img,255,255)

    #show the canny picture
    cv2.imshow("Window Name",edges)

     #save the canny picture First argument is the file name, second 
    argument is the image you want to save.
    cv2.imwrite('canny.png',edges)




      #making the image an array
    from PIL import Image
    #read the pciture
    img = Image.open('road.jpg').convert('LA')

     #save it
    img.save('greyscale.png') 
    #open the edited
     im=Image.open("greyscale.png")
     #make it an array
    pix_val = list(im.getdata())
    pix_val_flat = [x for sets in pix_val for x in sets]
    # pix_val_flat has the pixels for out first image without edging
    #print the array
    #print (pix_val_flat[125]);
    #get the lenght of the array
    lenght=len(pix_val_flat)
    #print the array
    #print(lenght);
    #take the canny picture and make it grayscale
    edge = Image.open('canny.png').convert('LA')
    #make it array
    pix_val1 = list(edge.getdata())
    pix_val_flat1 = [x for sets in pix_val for x in sets]
    #get the lenght of the array
    #lenght1=len(pix_val_flat1)
    #prnt the array
    #print(lenght);
    #print the array
    #print (pix_val_flat1[125]);

    print(lenght)

     newpic = [0]*lenght
    lenght2=len(newpic)
    print (newpic)

     for c1 in range(0,lenght,3):

         if pix_val_flat[c1]==pix_val_flat1[c1] and 
         pix_val_flat[c1+1]==pix_val_flat1[c1+1] and 
                        pix_val_flat[c1+2]==pix_val_flat1[c1+2]:
        newpic[c1]= pix_val_flat1[c1] 
        newpic[c1+1]= pix_val_flat1[c1+1]    
        newpic[c1+2]= pix_val_flat1[c1+2]  


     array = np.array(newpic, dtype=np.uint8)
     print (array)
     im2 = Image.new(im.mode, im.size)
     im2.putdata    (newpic)  ---------------------> here i get the error
     new_image = Image.fromarray(array)
     new_image.save('hello.png')


           cv2.waitKey(0)
      cv2.destroyAllWindows()
jornh
  • 1,369
  • 13
  • 27

1 Answers1

0

In this case it means that your putting more data than the size you set before. You can check the length of data you put in with len(the_list_of_data), so you'll see length gets double every time you put data (even if you overwrite). You can set the_list_of_data length to 0 and then fill it with data. This error occurs in loops too.