1

So I'm really new to this Python thing and I'm using JES and trying to figure out how to crop an image. I keep getting an error that "show (croppedPicture)" is invalid and I could use any help I can get at this point. This is my code so far:

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
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171

1 Answers1

0

Something seems to have gone a bit wrong with the pasting of your code; line 2 is missing it's indentation and there's a spurious "Add code here" string on line 3 (something odd has also gone wrong around the definition(s) of the crop() function as the indentation needs to be corrected before the code will run!).

That said, removing the crop() functions by deleting lines 29-43 as they aren't currently used, then the problem becomes easer to see ...

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

What's happening with the line target = makeEmptyPicture is that you're assign the variable target to the actual function makeEmptyPicture, not the what's returned by invoking that function.

For interest, there's more on how/why you'd actually want to do that in the post Assigning a function to a variable.

To fix this just add two parameters for the height and width of the cropped image to your call to makeEmptyPicture. N.B. you could just hard code a couple of numbers to check it's working first. I.e. target = makeEmptyPicture(50, 50)

Community
  • 1
  • 1
Jeremy Gosling
  • 1,130
  • 10
  • 11