Chapter 3 Study Guide
CSC 200 Study Guide – Fall 2010
Chapter 4 Modifying Pixels in a Range
Chapter Learning Objectives
The media learning goals for this chapter are:- To mirror pictures horizontally or vertically.
- To compose pictures into one another and create collages.
- To rotate pictures.
- To scale pictures smaller and larger.
- Use nested loops for addressing elements of a matrix.
- Loop through only part of an array.
- Develop some debugging strategies – specifically, to use print statements to explore executing code.
Study Smart, Not Hard
Take the entire week before the exam to study. It is impossible to cram all this information into your brain if you wait until night before the exam. Read all the short programs in this chapter to make sure that you understand exactly what they do. You should understand them much easier now than you did the third week of class.4.1 Copying Pixels
Program 19 def lighten(source):(page 75) This program goes through the entire source (picture object) one pixel at a time with the 2 for loops and uses the makeLighter built in function to lighten each pixel.
4.2 Mirroring a Picture
Program 20 def mirrorVertical(source):(page 77) See Figure 4.3
This program takes a picture object as the source and loops through the left half of the picture and copies all the pixels to the right half of the picture.
Program 21 def mirrorHorizontal(source):
(page 78) See Figure 4.4 (left image)
This program takes a picture object as the source and loops through the top half of the picture and copies all the pixels to the bottom half of the picture.
Program 22 def mirrorBotTop(source):
(page 78) See Figure 4.4 (right image)
This program takes a picture object as the source and loops through the bottom half of the picture and copies all the pixels to the top half of the picture.
Program 23 def mirrorTemple():
(Page 80)See Figure 4.6 and 4.7.
This program takes a picture object “temple.jpg” and copies the left portion of the roof area to the right side of the picture.
4.3 Copying and Transforming Pictures
In this section, you start with a picture object and a blank picture object and copy pixels from one to the other.Program 24: def copyBarb():
(Page 84) See Figure 4.8
This program copies the “barbara.jpg” picture to the top left corner of the “7inX95in.jpg” blank picture in memory.
Program 25: def copyBarb2():
(Page 86) See Figure 4.8
This program does the same thing as program 24. It just puts the target in the for loops and sets the source variables. Note: This program doesn’t really clarify anything. Just shows that you can write a program more than one way and it will still work correctly.
Program 26: def copyBarbMidway():
(Page 86-87) See Figure 4.9
This program copies the “Barbara.jpg” picture midway onto the blank “7inX95in.jpg” blank picture in memory starting at (100, 100) coordinate position.
Program 27: def copyBarbsFace():
(Page 87-88) See Figure 4.10
This program copies only the face of “Barbara.jpg” picture midway onto the blank “7inX95in.jpg” blank picture in memory. Her face starts at (45, 25) coordinate and ends at (200, 200) in the Barbara.jpg image.
Program 28: def copyBarbsFace2():
(page 89) See Figure 4.10
This program does the same thing as program 28. It just puts the target in the for loops and sets the source variables. Note: This program doesn’t really clarify anything. Just shows that you can write a program more than one way and it will still work correctly.
Program 29: def createCollage():
(Page 91 – 92) See Figure 4.12. This program is also available on the web site.
This program creates a collage of 5 flower pictures. We start with “flower1.jpg” and “flower2.jpg” and copy them to the bottom left corner of the blank canvas. Then we call the negative(flower1) function (from Program 16 page 66) to create the 3rd flower. We call the clearBlue(flower2) function (from Program 11 page 59) to create the 4th flower. Finally we call the decreaseRed(flowers1) function (page 63) to create the 5th flower. Note You must copy all 4 functions into the same file or load into memory for this to work.
Program 30: def copy(source, target, targX, targY):
(Page 30) This program is also available on the web site.
This program is a general copy function that will work for any picture object. You specify the source and target picture objects and the target x,y coordinate values.
Program 31 def CreateCollage2():
(Page 94) See Figure 4.12.
This program does the same thing as Program 29 except that it calls the copy function that we created 5 times instead of repeating the code. Note: This is a great example of the purpose of creating functions. It allows us to reuse our code.
Program 32 def copyBarbSideways():
(Page 95) See Figure 4.13.
This program rotates the “barbara.jpg” picture 90 degrees by simplying swapping the x and y values in the target.
Program 33 def rotateBarbSideways():
(Pages 96-97) See Figure 4.13.
This program creates the same result as Program 32. Note: The author acts as if it is a big difference but it really isn’t.
Program 34 copyBarbsFaceSmaller():
(Page 98) No figure.
This program is similar to Program 28. We are only copying a portion of the original “barbara.jpg” picture to the canvas and then we are scaling the picture down on the canvas. Note: Divide by 2 in the range and adding 2 to the X and Y source values.
Program 35 copyBarbsFaceLarger():
(Page 99) Figure 4.15.
This program does the same thing as Program 34 except we take every pixel in the source picture twice and copy it to the target picture so that we scale the picture to double its original size.
JES Statements
These statements can be typed in the Command area or included in a function in the Programming area.- for p in range (start, end, increment)
(pg 74) Loop through the values in the range starting with the start value and ending before the end value; increment by the increment value each time you go through the loop.
Example:for x in range(0, 150) - return picture
(page 82) If you are manipulating a picture then you should return the picture at the end so that the picture stays in memory after the function ends. - # Programmer: Carlotta Eaton
(page 84) Comments start with #. The jython compiler ignores these lines but they are very helpful for humans to understand the program.
Built-in JES functions
- range (start, end, increment)
Example: print range (0, 10, 2)
Output: [0, 2, 4, 6, 8]
- makeLighter(color)
(page 75) returns a lighter color than the input color - setMediaPath()
(Page 80) displays the file dialog box so that you can choose the folder where your .jpg files are located. - setMediaPath(“d:folder”)
(Page xx) lets you specify the complete path to the file in your code. - getMediaPath(“filename.jpg”)
(Page 80) appends the entire path to the filename so you do not have to include it in your code. You must use setMediaPath() from the command area or in your earlier code for this to work correctly. - makeEmptyPicture(width, height)
(Page 99) Creates a blank picture object with width and height given.
Jython / JES Programming Summary
See page 101 for the programming summary for this chapter.Last Update: January 13, 2019
Study Guide created by Carlotta Eaton For Introduction to Computing and Programming in Python – A Multimedia Approach by Guzdial & Ericson 2nd edition
CSC 200Page 1