TypeError: '>' not supported between instances of 'Image' and 'int'

TypeError: '>' not supported between instances of 'Image' and 'int'

我正在尝试编写一个 if 语句,如果列表索引超出范围,就会在适当的位置中断。 我不确定如何修复此错误。 我是 python 和 classess 的新手。 它在列表中保存为 PIL 图像。 错误:

/content/drive/MyDrive/triangles_pieces_dataset.py in slice_and_Create(self)
    349                             yield (horizontal_torch_neg, negative_label)
    350                             yield (vertical_torch_neg, negative_label)
--> 351                         if (listImagesTwo[l+2] > len(listImagesTwo)) or (listImages[k+1] > len(listImages)):
    352                           break
    353                         else:

TypeError: '>' not supported between instances of 'Image' and 'int'

发生错误的方法:

    def slice_and_Create(self):
        for folder in sample(os.listdir(self.root_dir), len(os.listdir(self.root_dir))):
            folder_path = self.root_dir + "/" + folder
            print(folder_path)
            for image in sample(os.listdir(folder_path), len(os.listdir(folder_path))):
                piece_coordinates, puzzle_pieces = self.visualize_the_triangle_image_sliced(folder_path + "/" + image)
                self.puzzle_pieces=puzzle_pieces
                self.piece_coordinates=piece_coordinates
                self.puzzle_pieces=puzzle_pieces
                puzzle_pieces=puzzle_pieces
                listImages = self.generate_triangles(puzzle_pieces)
                listImagesTwo = self.generate_triangles_two(puzzle_pieces)
                for k in range(len(listImages)): #Look into emmurate and the range 
                    for l in range(len(listImagesTwo)):
                        print("length",len(listImages))
                        print(len(listImagesTwo))
                        print(k)
                        print(l)
                        positive_label = 1
                        negative_label = 0
                        if listImages[k] and listImagesTwo[l]:
                            print(k)
                            print(l)
                            image_RGBA_new = listImages[k]
                            imageTwo_RGBA_v = listImagesTwo[l]
                          #  print("image",image_RGBA_new)
                          #  print(image_RGBA_v)
                            positive_past_torch = self.paste_image_positive_black(image_RGBA_new, imageTwo_RGBA_v)
                            vertical_torch_neg = self.vertical_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                            horizontal_torch_neg = self.horizonal_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                            yield (positive_past_torch, positive_label)
                            yield (horizontal_torch_neg, negative_label)
                            yield (vertical_torch_neg, negative_label)
                        if (listImagesTwo[l+2] > len(listImagesTwo)) or (listImages[k+1] > len(listImages)):
                          break
                        else:
                          if listImages[k] and listImagesTwo[l + 2]:
                              print(k)
                              print(l+2)
                              image_RGBA_new = listImages[k]
                              imageTwo_RGBA_v = listImagesTwo[l + 2]
                              vertical_torch = self.vertical_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                              horizontal_torch_neg = self.horizonal_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                              positive_torch_neg = self.paste_image_positive_black(image_RGBA_new, imageTwo_RGBA_v)
                              yield (vertical_torch, positive_label)
                              yield (horizontal_torch_neg, negative_label)
                              yield (positive_torch_neg, negative_label)

                          if listImages[k + 1] and listImagesTwo[l]:
                             print(k+1)
                             print(l)
                             image_RGBA_new = listImages[k+1] 
                             imageTwo_RGBA_v = listImagesTwo[l]
                             horizonatl_torch = self.horizonal_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                             vertical_torch_neg = self.vertical_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                             positive_torch_neg = self.paste_image_positive_black(image_RGBA_new, imageTwo_RGBA_v)
                             yield (vertical_torch_neg, negative_label)
                             yield (positive_torch_neg, negative_label)
                             yield (horizonatl_torch, positive_label)

有人可以帮我解决这个问题吗?

问题是由于

这样的说明造成的
if (listImagesTwo[l+2] > len(listImagesTwo))

您正在比较一个 PIL.Image 对象 (listImagesTwo[l+2]) 和一个 int (len(listImagesTwo))。正如错误消息所指出的那样。

有多种修复方法。在任何情况下,您已经在两个列表中循环,因此不需要 break 指令。