Python 类型错误:'&燃气轮机';在';图像';和';int';
我试图编写一个if语句,如果列表索引超出范围,就会出现中断。 我不确定如何修复此错误。 我不熟悉python和classess。 它将保存为列表中的PIL图像。 错误:Python 类型错误:'&燃气轮机';在';图像';和';int';,python,list,image,python-imaging-library,Python,List,Image,Python Imaging Library,我试图编写一个if语句,如果列表索引超出范围,就会出现中断。 我不确定如何修复此错误。 我不熟悉python和classess。 它将保存为列表中的PIL图像。 错误: /content/drive/MyDrive/triangles_pieces_dataset.py in slice_and_Create(self) 349 yield (horizontal_torch_neg, negative_label) 350
/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(listmagestwo[l+2]>len(listmagestwo))
您正在比较PIL.Image对象(listmagestwo[l+2]
)和int(len(listmagestwo)
)。正如错误消息指出的那样
有几种可能性可以修复。在任何情况下,您已经在两个列表上循环,因此不需要break指令。问题是,如果我没有break语句,则会导致索引超出范围错误。索引器/content/drive/MyDrive/triangles\u pieces\u dataset.py在slice\u和\u Create(self)中352#break 353#else:->354如果listImages[k]和listImagesTwo[l+2]:355打印(k)356打印(l+2)索引器:列表索引超出范围我的建议是仔细检查代码,在任何情况下,如果l+2>=len(listImagesTwo)或k+1>=len(listImages),您可以立即执行
:
。您希望比较索引,而不是列表元素