如何解决类型错误:“Image”和“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 指令。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。