错误(无限循环或其他原因)导致我的代码在 shell 中途停止 运行
Error (infinite looping or something else) causing my code to stop running in the shell partway through
虽然我不能确切地说出原因是什么,但我的代码中的某些东西导致它在中途停止 运行ning,使 shell 处于可以单击一行的状态,键入,按回车键等,但 shell 本身没有给我任何反应。
完整代码如下:
from PIL import Image
import PIL.ImageOps
background = "background.png"
target = input("Please enter the name of your image (including file type)")
targetImage = Image.open(target)
background = Image.open(background)
area = targetImage.size
backArea = background.size
area = (round(area[0] / 500), round(area[1] / 500))
backArea = (round(backArea[0] / 100), round(backArea[1] / 100))
targetImage = targetImage.resize(area)
background = background.resize(backArea)
size = area[0] * area[1]
targetWidth = area[0]
targetLength = area[1]
targetCoords = []
def analyser():
pixelOne = 1
pixelTwo = 1
completedSearch = 0
while completedSearch != size:
while pixelOne != targetWidth:
while pixelTwo != targetLength:
targetCoords.append((pixelOne, pixelTwo))
pixelTwo = pixelTwo + 1
completedSearch = completedSearch + 1
pixelTwo = 1
pixelOne = pixelOne + 1
colouredCoordsSum = []
largerColouredCoordsSum = []
smallerColouredCoordsSum = []
flatTargetCoords = [x for sets in targetCoords for x in sets]
coordLength = len(flatTargetCoords) - 1
for i in range(0, coordLength, 2):
firstnum = flatTargetCoords[i]
secondnum = flatTargetCoords[i+1]
sumnum = firstnum + secondnum
if firstNum > secondNum:
largerColouredCoordsSum.append("(",firstNum,", ",secondNum,"), (",sumnum,")")
else:
smallerColouredCoordsSum.append("(",firstNum,", ",secondNum,")")
colouredCoordsSum.append(sumnum)
global topLeft
global bottomRight
global topRight
global bottomLeft
topLeft = min(smallerColouredCoordsSum)
bottomRight = max(largerColouredCoordsSum)
topRight = topLeft + (area[0] - 1)
bottomLeft = bottomRight - (area[1] - 1)
analyser()
print(topLeft, topRight, bottomLeft, bottomRight)
我认为有问题的代码区域是第一个 while 循环的内容:
while completedSearch != size:
while pixelOne != targetWidth:
while pixelTwo != targetLength:
targetCoords.append((pixelOne, pixelTwo))
pixelTwo = pixelTwo + 1
completedSearch = completedSearch + 1
pixelTwo = 1
pixelOne = pixelOne + 1
也就是说,可能还有其他错误导致了此问题。由于此状态下 shell 中的代码未完全 运行,因此我无法在消除超过这一点的错误方面取得进展。任何帮助表示赞赏。
编辑:当我 运行 代码时,我得到了输入提示,但是在输入文件名并按回车后,我没有看到任何东西 运行,也没有看到'>>>' 在下一行,但我可以在空行之间单击,键入,然后在这些行上按回车键,但是 shell 不会对我在该状态下输入的任何内容做出反应。它应该在完成后将每个角的坐标打印到 shell,但它并没有发生。
while completedSearch != size:
while pixelOne != targetWidth:
while pixelTwo != targetLength:
targetCoords.append((pixelOne, pixelTwo))
pixelTwo = pixelTwo + 1
completedSearch = completedSearch + 1
pixelTwo = 1
pixelOne = pixelOne + 1
当这个循环开始时,如果completedSearch
大于size
,或者pixelOne
大于targetWidth
,或者pixelTwo
大于targetLength
,永远不会退出。
虽然我不能确切地说出原因是什么,但我的代码中的某些东西导致它在中途停止 运行ning,使 shell 处于可以单击一行的状态,键入,按回车键等,但 shell 本身没有给我任何反应。
完整代码如下:
from PIL import Image
import PIL.ImageOps
background = "background.png"
target = input("Please enter the name of your image (including file type)")
targetImage = Image.open(target)
background = Image.open(background)
area = targetImage.size
backArea = background.size
area = (round(area[0] / 500), round(area[1] / 500))
backArea = (round(backArea[0] / 100), round(backArea[1] / 100))
targetImage = targetImage.resize(area)
background = background.resize(backArea)
size = area[0] * area[1]
targetWidth = area[0]
targetLength = area[1]
targetCoords = []
def analyser():
pixelOne = 1
pixelTwo = 1
completedSearch = 0
while completedSearch != size:
while pixelOne != targetWidth:
while pixelTwo != targetLength:
targetCoords.append((pixelOne, pixelTwo))
pixelTwo = pixelTwo + 1
completedSearch = completedSearch + 1
pixelTwo = 1
pixelOne = pixelOne + 1
colouredCoordsSum = []
largerColouredCoordsSum = []
smallerColouredCoordsSum = []
flatTargetCoords = [x for sets in targetCoords for x in sets]
coordLength = len(flatTargetCoords) - 1
for i in range(0, coordLength, 2):
firstnum = flatTargetCoords[i]
secondnum = flatTargetCoords[i+1]
sumnum = firstnum + secondnum
if firstNum > secondNum:
largerColouredCoordsSum.append("(",firstNum,", ",secondNum,"), (",sumnum,")")
else:
smallerColouredCoordsSum.append("(",firstNum,", ",secondNum,")")
colouredCoordsSum.append(sumnum)
global topLeft
global bottomRight
global topRight
global bottomLeft
topLeft = min(smallerColouredCoordsSum)
bottomRight = max(largerColouredCoordsSum)
topRight = topLeft + (area[0] - 1)
bottomLeft = bottomRight - (area[1] - 1)
analyser()
print(topLeft, topRight, bottomLeft, bottomRight)
我认为有问题的代码区域是第一个 while 循环的内容:
while completedSearch != size:
while pixelOne != targetWidth:
while pixelTwo != targetLength:
targetCoords.append((pixelOne, pixelTwo))
pixelTwo = pixelTwo + 1
completedSearch = completedSearch + 1
pixelTwo = 1
pixelOne = pixelOne + 1
也就是说,可能还有其他错误导致了此问题。由于此状态下 shell 中的代码未完全 运行,因此我无法在消除超过这一点的错误方面取得进展。任何帮助表示赞赏。
编辑:当我 运行 代码时,我得到了输入提示,但是在输入文件名并按回车后,我没有看到任何东西 运行,也没有看到'>>>' 在下一行,但我可以在空行之间单击,键入,然后在这些行上按回车键,但是 shell 不会对我在该状态下输入的任何内容做出反应。它应该在完成后将每个角的坐标打印到 shell,但它并没有发生。
while completedSearch != size:
while pixelOne != targetWidth:
while pixelTwo != targetLength:
targetCoords.append((pixelOne, pixelTwo))
pixelTwo = pixelTwo + 1
completedSearch = completedSearch + 1
pixelTwo = 1
pixelOne = pixelOne + 1
当这个循环开始时,如果completedSearch
大于size
,或者pixelOne
大于targetWidth
,或者pixelTwo
大于targetLength
,永远不会退出。