【问题标题】:Error in Image Crop function in JythonJython 中的图像裁剪功能错误
【发布时间】:2013-06-09 02:30:04
【问题描述】:

好的,所以对于一个任务,我必须创建一个函数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."

谁能告诉我它有什么问题?和教科书上的代码是一样的。

【问题讨论】:

  • 您能复制并粘贴确切的错误消息吗?
  • 当然,它说:“参数值不正确(类型正确)。尝试将参数传递给函数时发生错误。”
  • 您使用的是什么版本的 jython?
  • JES(Jython 学生环境)

标签: image jython crop jes


【解决方案1】:

这行得通。我无法弄清楚您为什么会收到该错误,这可能与您如何获得 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

原图:

裁剪后的图像:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多