Test 1 Review- Spring 2008
Topics
Fundamentals of programming in JES and Python
- Explain basic terms such as algorithm, program, encoding, digitization. (Chapter 1)
- Distinguish between Python and JES. Explain JES command area and editor. (Chapter 2.2-2.3)
- Apply naming rules in Python to name variables or functions. Explain meaning of reserved words def, for, in, if, else. (Chapter 2.5, Chapter 3.3 )
Programming constructs
- Understand sequential execution of lines of code in blocks. (Chapter 2.5, Chapter 3.3)
- Use for loops in conjunction with getPixels to repeatedly execute a block of code. (Chapter 3.3)
- Use if and else to conditionally execute a block of code. (Chapter 4.4)
- Use def to define a function. (Chapter 2.5)
- Call (execute) functions in other functions. (Chapter 3.4)
- Explain what it means to pass an argument (parameter) into a function. (Chapter 2.5)
Pictures
- Demonstrate understanding of picture and RGB color encoding in JES. (Chapter 3.1)
- Understand the use of and difference between pickAFile and makePicture. (Chapter 3.2)
- Trace, explain, correct or write short functions to do pixel-by-pixel picture manipulations (e.g. negative, lighten, darken, color tinting, grayscale, sepia toning, posterizing) (Chapter 3)
– Note, you will NOT have to write any code from scratch in the test. You will either be given code to correct/trace, template (fill-in-the-blanks) code to complete, or will be given related code examples that you can use as hints.
Example Questions
We will be reading your answers. Here are the ground rules for the interaction.
- If you don't post, neither will we. We will not be posting official solutions to these problems at all! If one of you gets it right, terrific!
- We will try to always point out if an answer is wrong. We won't always point out when the answer is right.
- We are glad to work with you toward the right answer. We can give hints, and we're glad to respond to partial guesses.
If you post code, you must surround your code with <code> and </code> tags so that indentations will be preserved!
For example, to make your code appear correctly indented on the Coweb, you need to type something that looks like this:
<code>
def myFunction(a):
for line in range(1000):
print "I must remember that there are three bytes per pixel, not eight!"
</code>
We will not comment on code that is missing these code tags. We're not being personal or arbitrary about this; it's just something we all have to remember to do on the CoWeb to communicate effectively.
Conceptual Questions
15 Questions
- What does def mean? What does the statement def someFunction(x,y): do?
- What happens, then, when you type in the command area:
| >>> someFunction(2, 'fred') |
- Why is the maximum value of any color channel 255?
- The color encoding we're using is "RGB". What does that mean, in terms of the amount of memory required to represent color? (I.e. how many bytes are required to store a single pixel?)
- Is there a limit to the number of colors that we can represent in RGB? About how many colors can there be (or, alternately, how would you calculate the number?)
- Are there enough colors representable in RGB? (Can our output devices render more colors than this? Can our eyes/brain distinguish more colors?)
- What is an algorithm? Give an example of an algorithm that is not a computer program.
- If comments are ignored by the computer, why would you want to put comments in a Python program?
- What is an encoding? Give two examples of encodings of color in digitized pictures. (We are only using one of them in this course, but you should know that there are others.)
- Where do colons appear in Python code?
- If you have four lines of code, one starting with the keyword def, and three lines of code that define the function, how should the following three lines be indented?
- In "for p in getPixels(pic):" what does p refer to? If there are 1,000 pixels in the picture pic, how many times would the block following that line normally be executed?
- What is fundamentally wrong with saying "getRed gets all the red pixels"?
- What does it mean for a function to "take in" a value?
- If pic is a picture variable, what is the difference between show(pic) and print pic?
Picture Questions
Make Sunset
Consider this program from class, that makes an input picture look like it’s sunset:
def makeSunset(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p, value*0.7)
value = getGreen(p)
setGreen(p, value*0.7)
Why do we reduce the blue and green values instead of increasing the red values?
In each of the below situations, we show you a set of pixel RGB values on the top that you should presume are in some input picture, and what the RGB values should be after a given transformation function (like makeSunset above). Write the given function. [Note: this was from a longer test in a previous semester. We will not ask you to write code from scratch like this. However, we will test your knowledge of programming and color encoding by asking you to modify existing code or fill in the blanks in code templates, so this is a good practice exercise for you to do.]
(a)
Pixels coming in: (red, green, blue)
(10,25,75)
(250,19,100)
(55,200,5)
Pixels going out: (red, green, blue)
(0,25,75)
(0,19,100)
(0,200,5)
def functionA(picture):
(b)
Pixels coming in: (red, green, blue)
(10,25,75)
(250,19,100)
(55,200,5)
Pixels going out: (red, green, blue)
(10,75,25)
(250,100,19)
(55,5,200)
def functionB(picture):
(c)
Pixels coming in: (red, green, blue)
(10,25,75)
(250,19,100)
(55,200,5)
Pixels going out: (red, green, blue)
(10,10,10)
(250,250,250)
(55,55,55)
def functionC(picture):
Greyscale
Create a program that generates a greyscale photo. Write a function greyscale() which takes a picture as input. For each pixel, set the red, the green, and the blue values of that pixel to the average of the red, green, and blue values.
Here is a program for making a picture negative. You may find it useful guide when writing your grayscale function.
def negative(picture):
for pixel in getPixels(picture):
setRed(pixel, 255-getRed(pixel))
setGreen(pixel, 255-getGreen(pixel))
setBlue(pixel, 255-getBlue(pixel))
Generalized Color Changing
Here are a couple of functions, increaseRed and decreaseRed that increase and decrease, respectively, the red value of all pixels in a picture by 20%.
def decreaseRed(pic):
allThePixels = getPixels(pic)
for eachPixel in allThePixels:
value = getRed(eachPixel)
setRed(eachPixel, value*0.8)
def increaseRed(pic):
allThePixels = getPixels(pic)
for eachPixel in allThePixels:
value = getRed(eachPixel)
setRed(eachPixel, value*1.2)
Write a new function called changeColor that does both and more. It takes as its input a picture AND an amount to increase or decrease a color by (a number between -.99 and .99) AND the word 'red', 'green', or 'blue'.
Here are some examples of what your function should be able to do...
- changeColor(pic, -.10, 'red') should decrease the amount of red in the picture by 10%.
- changeColor(pic, .30, 'green') should increase the amount of green in the picture by 30%
- changeColor(pic, 0, 'blue') should do nothing at all to the amount of blue in the picture (or red or green for that matter).
Generalized Image Filter
Now go one step further. Write a function called changeRGB that takes four inputs: A picture, and an amount to change the red, green, and blue (in that order) pixels. Like before, each amount should be between -.99 and .99.
Thus, changeRGB(picture, -.75, .25, -.25) would...
- Decrease the red by 75%
- Increase the green by 25%
- Decrease the blue by 25%
Hint: The blast function in the codes/slides page is similar. Use it for inspiration if you are stuck.
Questions?
Link to this Page
- Hotspots #3 last edited on 5 May 2008 at 10:40 am by c-76-17-124-0.hsd1.ga.comcast.net