【问题标题】:Cropping an image in JES (Python)在 JES (Python) 中裁剪图像
【发布时间】:2015-05-10 01:55:42
【问题描述】:

所以我对这个 Python 东西真的很陌生,我正在使用 JES 并试图弄清楚如何裁剪图像。我不断收到“显示(裁剪图片)”无效的错误消息,此时我可以使用我能获得的任何帮助。到目前为止,这是我的代码:

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

【问题讨论】:

    标签: python image jython crop jes


    【解决方案1】:

    粘贴代码时似乎出了点问题;第 2 行缺少缩进,第 3 行有一个虚假的“在此处添加代码”字符串(crop() 函数的定义也出现了一些奇怪的问题,因为在代码运行之前需要更正缩进!)。

    也就是说,通过删除第 29-43 行来删除crop() 函数,因为它们当前没有被使用,那么问题就变得更容易看到了......

       def makeCroppedPicture(pict, startX, startY, endX, endY):
           target = makeEmptyPicture
           return target   # returns the cropped picture
    

    target = makeEmptyPicture 行发生的情况是您将变量目标分配给实际函数 makeEmptyPicture,而不是调用该函数返回的内容。

    出于兴趣,您可以在Assigning a function to a variable 帖子中详细了解您实际想要这样做的方式/原因。

    要解决此问题,只需在调用 makeEmptyPicture 时添加裁剪图像的高度和宽度两个参数。注:你可以硬编码几个数字来检查它是否首先工作。 IE。 target = makeEmptyPicture(50, 50)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 2020-10-30
      相关资源
      最近更新 更多