Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 1 Review Spring 2004: Scaling with input

(Return to Sp2004 Midterm 1 Review)

Questions, answers, comments on answers, comments on questions?



I've had some questions on this one along the lines,"What's that int thing?" Some examples to help it make sense. Mark Guzdial
>>> print int(12.1)
12
>>> print (12.9)
12.9
>>> print 12.9/2.0
6.45
>>> print 12/2.0
6.0
>>> print int(12/2.0)
6
>>> print 12/2.0 = int(12/2.0)
Your code contains at least one syntax error, meaning it is not legal jython.
>>> print 12/2.0 == int(12/2.0)
1
>>> print 13/2.0
6.5
>>> print int(13/2.0)
6
>>> print 13/2.0 == int(13/2.0)
0


Student970
This is the message I got when I pasted the barb function into JES and tried to run it. What's the problem?

Note: There is no file at .\barbara.jpg
makePicture(filename): There is no file at .\.\barbara.jpg
An error occurred attempting to pass an argument to a function.
Please check line 4 of C:\JES\midterm practice 12.py

Did you do setMediaPath() first? Mark Guzdial


this code reads this error when trying to run it in JES:

Copying from 45 25 to 100 100 A local or global name could not be found. You need to define the function or variable before you try to use it in any way. Please check line 34 of C:\Documents and Settings\Mary Kate Ficke\My Documents\testme

def copyBarbsFaceScaled(factor):
  # Set up the source and target pictures
  barbf = getMediaPath("barbara.jpg")
  barb = makePicture(barbf)
  canvasf = getMediaPath("7inX95in.jpg")
  # Now, do the actual copying
  sourceX = 45
  for targetX in range(100,100+int((200-45)*factor)):
    sourceY = 25
    for targetY in range(100,100+int((200-25)*factor)):
      print "Copying from", sourceX,sourceY,"to",targetX,targetY
      color = getColor(getPixel(barb,int(sourceX),int(sourceY)))
      setColor(getPixel(canvas,targetX,targetY), color)
      sourceY = sourceY + (1.0/factor)
    sourceX = sourceX + (1.0/factor)
  show(barb)
  show(canvas)
  return canvas

did i miss something? sorry about not reading the faq...line 34 is the setColor line.

Did you setMediaPath() in the command area first?
There aren't 34 lines there, Mary Kate, so it's hard to figure it out. Try turning on the Expert mode (in the Options item, in the Edit menu) and see what word it's complaining about. Oh wait, now I see it. Yeah, you forgot the line where you set canvas equal to makePicture(canvasf) Mark Guzdial

I am still working on figuring out what first four lines will print. Before I do I want to make sure I am on the right track. (I'm tracing the programs before I attempt to run them...)

Since the increment is +1.0/factor, this means when the factor is 0.5 the increment is by +2 so the size of the canvas (target) picture is half the size of the source. When the factor is 2.0 the increment is by +0.5 so the size of the canvas picture is double the size of the source. When the factor is 1.5 the increment is +2/3 so the size of the canvas picture is 2/3 (or 0.66667) greater than the size of the source. Am I on the right track? Thanks Andrea
If you add 1/2 to the sourceX (or sourceY), you end up taking each pixel twice, thus doubling (in each dimension) the size of the picture. I think you're on the right track, Andrea. Mark Guzdial

Answer to A is: that portion of Barbara's face that begins at 45 25 scaled down by half.
Copying from 45 25 to 100 100
Copying from 45 27 to 100 101
Copying from 45 29 to 100 102
Copying from 45 31 to 100 103

B is similar only double the size with a different set of copying from y values. Likewise, C increases the size by 1.5.

Is this on the right track? Student1295

Here's my answer for all of it:

A) factor of 0.5 means you're scaling down: I got what REMOVED got

B) factor of 2 means you're scaling up:
Copying from 45 25 to 100 100
Copying from 45 25 to 100 101
Copying from 45 26 to 100 102
Copying from 45 26 to 100 103
Copying from 45 27 to 100 104
Copying from 45 27 to 100 105
Copying from 45 28 to 100 106
Copying from 45 28 to 100 107

C. factor of 1.5 means you're scaling up the picture 2/3 - this means you take every other pixel from the source and go over to the canvas and copy it to three pixels:
Copying from 45 25 to 100 100
Copying from 45 25 to 100 101
Copying from 45 25 to 100 102
Copying from 45 27 to 100 103
Copying from 45 27 to 100 104
Copying from 45 27 to 100 105
Copying from 45 29 to 100 106
Copying from 45 29 to 100 107
Copying from 45 29 to 100 108
Copying from 45 31 to 100 109
Copying from 45 31 to 100 110
Copying from 45 31 to 100 111

Note 1/1.5 = 0.666666667 or 2/3 that's how I came to 2/3.

That's my story and I'm sticking to it. (well that's my story anyway!) Now I'll run it to see how wrong I might be! Andrea


Follow up from above, it does increment the sourceY differently although the picture you see is the same (in the end)...

So the lines print like this (for B and the same idea for C):

Copying from 45 25.0 to 100 100
Copying from 45 25.5 to 100 101
Copying from 45 26.0 to 100 102
Copying from 45 26.5 to 100 103
Copying from 45 27.0 to 100 104
Copying from 45 27.5 to 100 105
Copying from 45 28.0 to 100 106
Copying from 45 28.5 to 100 107

Same thing for C - it increments by .66666667 (2/3) from SourceY.

Andrea

REMOVEDmmary of the information provided above and my own information:

A. It decreases by half.
Copying from 45 25 to 100 100
Copying from 45 27 to 100 101
Copying from 45 29 to 100 102
Copying from 45 31 to 100 103

B. It increases the picture size by 2.
Copying from 45 25.0 to 100 100
Copying from 45 25.5 to 100 101
Copying from 45 26.0 to 100 102
Copying from 45 26.5 to 100 103

C. The picture size will increase by a factor of 1.5.
Copying from 45 25.00 to 100 100
Copying from 45 25.67 to 100 101
Copying from 45 26.33 to 100 102
Copying from 45 27.00 to 100 103

I think that's right, judging from everything so far...yell at me if I'm wrong! Or ignore me. Either works.

Christina

I am really really really lost!! It says to print "copying from.." how is it changing the source X and source Y???? And, if I put in 0.5 as the factor into the problem 100+int((200-45)*0.5) I get 100+77...which is 177..where are people getting 100 from!? :(Kyla LeCroy

woah, I have no idea what just happened there...sorry
Kyla LeCroy: 
_
i just ran this in jes and it does not require that the source X be an integer...christina is right.Melanie REMOVED

sourceX doesn't have to be an integer, because we use the int() function to turn it into an integer when we get the color (see inside the getPixel function call). Kyla, the 100 is from the targetX and targetY, not the source. Mark Guzdial


def copyBarbsFaceScaled(factor):
  barbf=getMediaPath("barbara.jpg")
  barb= makePicture(barbf)
  canvasf=getMediaPath("7inX95in.jpg")
  canvas=makePicture(canvasf)
  sourceX=45
  for targetY in range  (100,100+int((200-45)*factor)):
    sourceY=25
    for targetY in range (100,100+int((200-25)*factor)):
    #   print "Copying from" ,sourceX,sourceY, "to" ,targetX,targetY,
       color= getColor (getPixel (barb, int(sourceX), int(sourceY)))
       setColor (getPixel (canvas, targetX, targetY), color)
       sourceY= sourceY+ (1.0/factor)
    sourceX=sourceX+(1.0/factor)
  show(barb)
  show(canvas)
  return canvas
Student1165 I already set the path and what not. but jes keeps telling me to check the line that has setColor.. what should i do?
You really do have to read the error. The error that you got here was about a variable not being found. Look at your for loops – you have targetY in both. There is NO targetX – that's the variable that's not found, and that's why you're getting the error. Mark Guzdial

yeah me too...the setColor line...not cool



The setColor line has NOTHING to do with setMediaPath or getMediaPath. If you're getting outOfBounds errors, it's because you are going beyond the edge of the picture. If you're getting it in the setColor line, it's probably the case targetX or targetY is going beyond the end of the canvas. For example, you can't use a factor much past 2.0 or you'll be going beyond the end of the canvas. (In one of my Breakout sections, we added a piece of code so that the target would crop the picture if the scaling got too large – try it! It's not hard!) Mark Guzdial



Will somebody confirm if the int function effects what the program prints to an integer?! Because right now we have two different answers up and no one has said if one of them is wrong.




Link to this Page