TypeError:“>”在“Image”和“int”的实例之间不受支持

2024-05-29 02:51:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编写一个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)

有人能帮我修一下吗


Tags: imageselfnewtorchlabelprintyieldvertical
1条回答
网友
1楼 · 发布于 2024-05-29 02:51:50

该问题是由以下说明引起的:

if (listImagesTwo[l+2] > len(listImagesTwo))

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

有几种可能性可以修复。在任何情况下,您已经在两个列表上循环,因此不需要break指令

相关问题 更多 >

    热门问题