1

Okay, so for an assignment I have to make a function crop() that will crop a picture in test_crop(). Here's the code.

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)

The error occurs on this piece of code:

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

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

Can someone please tell me what is wrong with it? It is the same code from the textbook.

user2387191
  • 65
  • 1
  • 4
  • 9

1 Answers1

0

This works. I cannot work out why you are getting that error, it may be in how you are getting your values for startX, startY, endX, endY in 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

The original image:

Original picture

The cropped image:

Cropped Picture

This works, beyond this, you will need to provide more information about your code.

Community
  • 1
  • 1