1

好的,所以对于一项任务,我必须创建一个可以crop()test_crop(). 这是代码。

def crop(pict, startX, startY, endX, endY):
 width = endX - startX + 1
 height = endY - startY + 1
 canvas = makeEmptyPicture(width, height)
 targetX = 100
 for sourceX in range(45,200):
  targetY = 100
  for sourceY in range(25,200):
    color = getColor(getPixel(pict, sourceX, sourceY))
    setColor(getPixel(canvas, targetX, targetY), color)
    targetY = targetY + 1
  targetX = targetX + 1
show(pict)
show(canvas)
return canvas

def test_crop():
 setMediaPath() 
 pict = makePicture("redMotorcycle.jpg")
 croppedPict = crop(pict, 100, 100, 700, getHeight(pict)/2)
 show(pict)
 show(croppedPict)

这段代码出现错误:

setColor(getPixel(canvas, targetX, targetY), color)

它说"Inappropriate argument (of correct type. An error occured attempting to pass an argument to a function."

有人可以告诉我它有什么问题吗?它与教科书中的代码相同。

4

1 回答 1

0

这行得通。我无法弄清楚您为什么会收到该错误,这可能与您如何获得价值startX, startY, endX, endY有关 def crop(pict, startX, startY, endX, endY):

def crop(pict):
  width = getWidth(pict)
  height = getHeight(pict)
  canvas = makeEmptyPicture(width, height)
  targetX = 100
  for sourceX in range(45,200):
    targetY = 100
    for sourceY in range(25,200):
      color = getColor(getPixel(pict, sourceX, sourceY))
      setColor(getPixel(canvas, targetX, targetY),color)
      targetY = targetY + 1
    targetX = targetX + 1
  show(pict)
  show(canvas)
  return canvas

原图:

原图

裁剪后的图像:

裁剪图片

这有效,除此之外,您将需要提供有关您的代码的更多信息。

于 2013-07-17T12:58:17.807 回答