虽然我不能确切地说出原因是什么,但代码中的某些内容导致它中途停止运行,使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不会对我在该状态下键入的任何内容做出反应。完成后,应将每个角点的坐标打印到壳上,但不会出现这种情况。你知道吗
当这个循环开始时,如果
completedSearch
大于size
,或者pixelOne
大于targetWidth
,或者pixelTwo
大于targetLength
,它将永远不会退出。你知道吗相关问题 更多 >
编程相关推荐