0

我正在尝试重新创建图片。我拍了一张边缘它的照片并保存它。在我把它变成灰度并保存之后。找到了两个图像的共同像素,我试图重新创建图片,我得到了这个错误。这是一张道路的照片,我试图只保留白色的车道。因此,在我将边缘图片与第一张图片进行比较之后,最常见的像素是代表道路车道的白色像素。

错误在代码清单末尾附近标记为 <------ 的行中抛出

TypeError: too many data entries 

newpic 是这种形式的列表 `[1,1,1,1,...,1]

这是我的代码并解释了每一部分。如果您有任何其他建议如何达到我想要的结果,请说出来

    #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()
4

1 回答 1

0

在这种情况下,这意味着您放置的数据量超过了您之前设置的大小。您可以使用 len(the_list_of_data) 检查输入的数据长度,因此每次输入数据时都会看到长度加倍(即使您覆盖)。您可以将 the_list_of_data 长度设置为 0,然后用数据填充它。此错误也发生在循环中。

于 2019-05-31T22:01:24.707 回答