所以我对这个Python事物真的很陌生,我正在使用JES并试图弄清楚如何裁剪图像。我一直收到“show(croppedPicture)”无效的错误,我可以使用我现在可以获得的任何帮助。到目前为止,这是我的代码:
def main():
print "Select the Media Folder"
setMediaFolder()
print "Select the picture (.jpg) file to crop"
fileName = pickAFile()
pict = makePicture(fileName)
show(pict)
startX = requestIntegerInRange("Enter X coordinate of upper, left-hand corner",0,getWidth(pict)-1)
startY = requestIntegerInRange("Enter Y coordinate of upper, left-hand corner",0,getHeight(pict)-1)
endX = requestIntegerInRange("Enter X coordinate of lower, right-hand corner",startX,getWidth(pict)-1)
endY = requestIntegerInRange("Enter Y coordinate of lower, right-hand corner",startY,getHeight(pict)-1)
print "Please wait while picture is cropped from (",startX,",",startY,") to (",endX,",",endY,")."
croppedPicture = makeCroppedPicture(pict, startX, startY, endX, endY)
show(croppedPicture)
newFileName = getMediaPath('croppedPicture.jpg')
writePictureTo(croppedPicture, newFileName)
def makeCroppedPicture(pict, startX, startY, endX, endY):
""" Makes and returns a cropped rectangular region of a picture into a new picture """
target = makeEmptyPicture
def crop(picture):
def crop(picture):
width = getWidth(pict)
height = getHeight(pict)
canvas = makeEmptyPicture(width, height)
targetX = 100
for sourceX in range(100,30):
targetY = 100
for sourceY in range(311,433):
color = getColor(getPixel(pict, sourceX, sourceY))
setColor(getPixel(canvas, targetX, targetY),color)
targetY = targetY + 1
targetX = targetX + 1
show(pict)
return canvas
return target # returns the cropped picture
main() # starts the program
答案 0 :(得分:0)
粘贴代码似乎有些不对劲;第2行缺少它的缩进,并且在第3行有一个虚假的“在这里添加代码”字符串(在crop()函数的定义周围出现了一些奇怪的错误,因为在代码运行之前需要纠正缩进!)。
也就是说,通过删除当前未使用的第29-43行删除crop()函数,然后问题变得更加容易看到...
def makeCroppedPicture(pict, startX, startY, endX, endY):
target = makeEmptyPicture
return target # returns the cropped picture
行target = makeEmptyPicture
发生的是你将变量target分配给实际的函数makeEmptyPicture,而不是通过调用该函数返回的内容。
有兴趣的话,关于你如何/为什么要在帖子Assigning a function to a variable中真正想要这样做的更多内容。
要解决这个问题,只需在调用makeEmptyPicture时为裁剪后的图像的高度和宽度添加两个参数。注:你可以硬编码几个数字来检查它是否正常工作。即target = makeEmptyPicture(50, 50)