在JES中裁剪图像(Python)

2024-09-30 18:13:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用一个新的图像来裁剪。我一直得到一个错误,“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

Tags: ofthecoordinatedefshowenterhandpicture
1条回答
网友
1楼 · 发布于 2024-09-30 18:13:44

粘贴代码时似乎有点不对劲;第2行缺少缩进,第3行有一个伪的“Add code here”字符串(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调用中添加两个裁剪图像的高度和宽度参数。N、 你可以先硬编码几个数字来检查它是否有效。一、 e.target = makeEmptyPicture(50, 50)

相关问题 更多 >