虽然我不能确切地说出原因是什么,但我的代码中有一些东西导致它中途停止运行,使shell处于可以单击一行、键入、按enter等的状态,但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中完全运行,我无法在这一点之后删除错误。感谢您的帮助。
编辑:当我运行代码时,我会得到输入提示,但在键入文件名并按enter键后,我看不到任何运行内容,也看不到下一行的">>",但我可以在空行之间单击,键入并在这些行上按enter键,但在这种状态下,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
,它将永远不会退出。