images (part 1) - stanford university · images (part 1) cs106ap lecture 8. roadmap programming...
TRANSCRIPT
![Page 1: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/1.jpg)
Images (Part 1)CS106AP Lecture 8
![Page 2: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/2.jpg)
RoadmapProgramming Basics
The Console Images
Data structures
MidtermGraphics
Object-Oriented Programming
Everyday Python
Life after CS106AP!
Day 1!
![Page 3: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/3.jpg)
The Console
Data structures
Everyday PythonObject-Oriented Programming
MidtermGraphics
Programming Basics
Roadmap
Life after CS106AP!
Day 1!Images
Images
Advanced Images
![Page 4: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/4.jpg)
Today’s questions
How do computers store images?
How can we manipulate images through code?
![Page 5: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/5.jpg)
Today’s topics
1. Review
2. Introduction to images
3. Another for loop
4. What’s next?
![Page 6: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/6.jpg)
Review
![Page 7: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/7.jpg)
Console programs
![Page 8: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/8.jpg)
Console programA program that solicits input from a user via an
interactive terminal (console) and does something interesting with that input
Definition
![Page 9: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/9.jpg)
Console program summary
● Use input(prompt) to read in information from the user.○ Make sure to convert the data to the correct type (from the
string data type)!
● Use print() to display information for the user.○ Make sure to convert the data to the correct type (from the
int/float data type)
● Use a while loop to enable multiple runs of your program.
![Page 10: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/10.jpg)
Strings
![Page 11: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/11.jpg)
stringA data type that represents a sequence of
characters
Definition
![Page 12: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/12.jpg)
stringA data type that represents a sequence of
characters
Definition
Characters can be letters, digits, symbols (&, !, ~), etc.
![Page 13: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/13.jpg)
String fundamentals
● String literals are any string of characters enclosed in single (‘’) or double quotes (“”)
● Each character in the string is associated with an index
● Strings can be combined with the + operator in a process called concatenation
● Strings are immutable
![Page 14: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/14.jpg)
Indexing and slicing
● Length○ The length of a string is the number of characters it contains○ We can use the Python function len() to evaluate the length of a
string
len(‘banana’) → 6len(‘’) → 0len(‘CS106AP rocks my socks’) → 22
![Page 15: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/15.jpg)
Slides courtesy of Sam Redmond, CS41
![Page 16: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/16.jpg)
Slides courtesy of Sam Redmond, CS41
![Page 17: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/17.jpg)
Slides courtesy of Sam Redmond, CS41
What are the correct indices?
![Page 18: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/18.jpg)
Slides courtesy of Sam Redmond, CS41
![Page 19: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/19.jpg)
Slides courtesy of Sam Redmond, CS41
![Page 20: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/20.jpg)
String functions
● All follow the noun.verb() syntax we’ve seen before
● str.isupper(), str.islower()
● str.isalpha(), str.isdigit()
● str.upper(), str.lower()
![Page 21: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/21.jpg)
String functions
● All follow the noun.verb() syntax we’ve seen before
● str.isupper(), str.islower()
● str.isalpha(), str.isdigit()
● str.upper(), str.lower()
Return True or False
![Page 22: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/22.jpg)
String functions
● All follow the noun.verb() syntax we’ve seen before
● str.isupper(), str.islower()
● str.isalpha(), str.isdigit()
● str.upper(), str.lower() Return updated string
![Page 23: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/23.jpg)
String functions
● All follow the noun.verb() syntax we’ve seen before
● str.isupper(), str.islower()
● str.isalpha(), str.isdigit()
● str.upper(), str.lower() Return updated string
Remember: Original string is unchanged because of immutability!
![Page 24: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/24.jpg)
Type conversion
● Important note: ‘123’ is a string and 123 is an int
● In order to convert between data types, we can use built-in Python functions: str(), int(), float()
int(‘123’) == 123float(‘24.7’) == 24.7str(12345) == ‘12345’str(20.19) == ‘20.19’
![Page 25: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/25.jpg)
Doctests
![Page 26: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/26.jpg)
Doctests
● Python has a great testing framework called doctests○ For each function in your program, write doctests that specify an
output for a given input○ You can (and should) have multiple doctests per function
![Page 27: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/27.jpg)
Doctests
● Python has a great testing framework called doctests
● PyCharm supports doctests by allowing you to easily run them in the editor○ Put doctests in function header comments using `>>>`
![Page 28: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/28.jpg)
Doctests
● Python has a great testing framework called doctests
● PyCharm supports doctests by allowing you to easily run them in the editor○ Put doctests in function header comments using `>>>`
def add(a, b):“““>>> add(2, 4)6”””...
![Page 29: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/29.jpg)
Doctests
● Python has a great testing framework called doctests
● PyCharm supports doctests by allowing you to easily run them in the editor○ Put doctests in function header comments using `>>>`
def add(a, b):“““>>> add(2, 4)6”””...
Call the function and specify any arguments if needed.
![Page 30: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/30.jpg)
Doctests
● Python has a great testing framework called doctests
● PyCharm supports doctests by allowing you to easily run them in the editor○ Put doctests in function header comments using `>>>`
def add(a, b):“““>>> add(2, 4)6”””...
Put the expected output directly after the test.
![Page 31: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/31.jpg)
Testing strategies
● Write tests that cover a wide variety of use cases for your function!
● Consider:○ Basic use cases○ Edge cases
edge caseUses of your function/program that
represent extreme situations
Definition
![Page 32: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/32.jpg)
EliminationNation.py[demo]
![Page 33: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/33.jpg)
Takeaways
● Common pattern: processing all characters in a string
for i in range(len(s)):current_char = s[i]# Use current_char
![Page 34: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/34.jpg)
Takeaways
● Common pattern: processing all characters in a string
● Common pattern: building up a new string
new_string = ‘’for i in range(len(s)):
new_string += s[i]
![Page 35: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/35.jpg)
Takeaways
● Common pattern: processing all characters in a string
● Common pattern: building up a new string
new_string = ‘’for i in range(len(s)):
if __________:new_string += s[i]
![Page 36: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/36.jpg)
Takeaways
● Common pattern: processing all characters in a string
● Common pattern: building up a new string
new_string = ‘’for i in range(len(s)):
if __________:new_string += s[i]
Select only certain characters - think
of this as a filter!
![Page 37: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/37.jpg)
Takeaways
● Common pattern: processing all characters in a string
● Common pattern: building up a new string
● Write doctests for every function!○ Cover a range of usage patterns for your function○ Write them before writing the actual function code○ Run them often as you make changes
![Page 38: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/38.jpg)
Takeaways
● Common pattern: processing all characters in a string
● Common pattern: building up a new string
● Write doctests for every function!○ Cover a range of usage patterns for your function○ Write them before writing the actual function code○ Run them often as you make changes
![Page 39: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/39.jpg)
What is an image?
![Page 40: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/40.jpg)
What is an image?
● An image is made up of square pixels
[Preview demo]
![Page 41: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/41.jpg)
What is an image?
● An image is made up of square pixels
● Each pixel has x and y coordinates depending on its location in the image○ The origin (0, 0) is at the upper left○ y increases going down, x increases going right
x
y
(0,0)
![Page 42: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/42.jpg)
What is an image?
● An image is made up of square pixels
● Each pixel has x and y coordinates depending on its location in the image
● Each pixel has a single color, encoded as three RGB numbers○ R = red; G = green; B = blue○ Each value represents a brightness for that color (red, green, or
blue)○ You can use these three colors to make any color!
![Page 43: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/43.jpg)
What is an image?
● An image is made up of square pixels
● Each pixel has x and y coordinates depending on its location in the image
● Each pixel has a single color, encoded as three RGB numbers
[RGB explorer demo]
![Page 44: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/44.jpg)
What is an image?
Image courtesy of Nick Parlante
![Page 45: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/45.jpg)
How can we manipulate images with code?
![Page 46: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/46.jpg)
SimpleImage module
![Page 47: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/47.jpg)
SimpleImage module
● Import the module
from simpleimage import SimpleImage
![Page 48: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/48.jpg)
SimpleImage module
● Import the module
from simpleimage import SimpleImage
NOTE: The module may not work for you yet due to some installation requirements!
(We’ve posted a SimpleImage reference handout on the course website with instructions on how to set up and use the module.)
![Page 49: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/49.jpg)
SimpleImage module
● Import the module
● Create a SimpleImage object and store it in a variable○ Each SimpleImage object is made up of Pixel objects
image = SimpleImage(filename)
![Page 50: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/50.jpg)
SimpleImage module
● Import the module
● Create a SimpleImage object and store it in a variable
● Show the image on your computer
image.show()
![Page 51: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/51.jpg)
SimpleImage module
● Import the module
● Create a SimpleImage object and store it in a variable
● Show the image on your computer
● Idea: We manipulate images by editing their pixels!
![Page 52: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/52.jpg)
SimpleImage module
● Import the module
● Create a SimpleImage object and store it in a variable
● Show the image on your computer
● Idea: We manipulate images by editing their pixels!
How do we access pixels?
![Page 53: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/53.jpg)
For each loops
![Page 54: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/54.jpg)
For each loops
A new type of for loop!
![Page 55: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/55.jpg)
Recall: For range() loops
for i in range(end_index):# assumes 0 is the start indexdo_something()
for i in range(start_index, end_index):# end_index is not inclusive!# recall: range(4,7) -> 4,5,6do_something()
![Page 56: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/56.jpg)
For each loops
for item in collection:
# Do something with item
![Page 57: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/57.jpg)
For each loops
image = SimpleImage(‘flower.jpg’)
for pixel in image:
# Do something with pixel
![Page 58: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/58.jpg)
For each loops
image = SimpleImage(‘flower.jpg’)
for pixel in image:
# Do something with pixel
Like the i in for range() loops, pixel is a variable that gets
updated with each loop iteration
![Page 59: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/59.jpg)
For each loops
image = SimpleImage(‘flower.jpg’)
for pixel in image:
# Do something with pixel
pixel gets assigned to each pixel object in the image in turn
![Page 60: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/60.jpg)
For each loops
image = SimpleImage(‘flower.jpg’)
for pixel in image:
# Do something with pixel
This code gets repeated once for each pixel in the image
![Page 61: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/61.jpg)
Let’s make Photoshop![demo]
![Page 62: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/62.jpg)
Summary
● Use a for each loop to loop over all pixels in an image
![Page 63: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/63.jpg)
Summary
● Use a for each loop to loop over all pixels in an image
● Edit a pixel by updating its properties:○ pixel.x, pixel.y → coordinates○ pixel.red, pixel.green, pixel.blue → RGB values
■ A higher R, G, or B value means a greater amount of that color
![Page 64: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/64.jpg)
Summary
● Use a for each loop to loop over all pixels in an image
● Edit a pixel by updating its properties:○ pixel.x, pixel.y → coordinates○ pixel.red, pixel.green, pixel.blue → RGB values
■ A higher R, G, or B value means a greater amount of that color
● Each SimpleImage also has properties:○ image.width → maximum x value○ image.height → maximum y value
![Page 65: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/65.jpg)
Summary
● Use a for each loop to loop over all pixels in an image
● Edit a pixel by updating its properties:○ pixel.x, pixel.y → coordinates○ pixel.red, pixel.green, pixel.blue → RGB values
■ A higher R, G, or B value means a greater amount of that color
● Each SimpleImage also has properties:○ image.width → maximum x value○ image.height → maximum y value
![Page 66: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/66.jpg)
Think/Pair/ShareHow would you darken only the bottom right quadrant?
![Page 67: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/67.jpg)
Grayscale algorithm[demo]
![Page 68: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/68.jpg)
Grayscale algorithm
● You get the color “gray” in a pixel when its red, green, and blue values are all the same.
● To keep grayscale an image, average the red, green, and blue values for a given pixel and re-assign each RGB value in that pixel to the average.
![Page 69: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/69.jpg)
curb_repair()[demo]
![Page 70: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/70.jpg)
Greenscreen algorithm[demo]
![Page 71: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/71.jpg)
Greenscreen (or bluescreen) algorithm
● This is how green-screening in movies works!
for pixel in image:
Loop over all pixels in the image
![Page 72: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/72.jpg)
Greenscreen (or bluescreen) algorithm
● This is how green-screening in movies works!
for pixel in image: average = (pixel.red + pixel.green + pixel.blue) // 3
Average the RGB values for the pixel
![Page 73: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/73.jpg)
Greenscreen (or bluescreen) algorithm
● This is how green-screening in movies works!
for pixel in image: average = (pixel.red + pixel.green + pixel.blue) // 3 if pixel.red >= average * 1.6:
Filter for pixels whose red value is above the average times some “hurdle factor” (i.e. find “red-enough” pixels!)
![Page 74: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/74.jpg)
Greenscreen (or bluescreen) algorithm
● This is how green-screening in movies works!
for pixel in image: average = (pixel.red + pixel.green + pixel.blue) // 3 if pixel.red >= average * 1.6: # the key line: pixel_back = back.get_pixel(pixel.x, pixel.y)
Get the corresponding pixel from the “background” image
![Page 75: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/75.jpg)
Greenscreen (or bluescreen) algorithm
● This is how green-screening in movies works!
for pixel in image: average = (pixel.red + pixel.green + pixel.blue) // 3 if pixel.red >= average * 1.6: # the key line: pixel_back = back.get_pixel(pixel.x, pixel.y) pixel.red = pixel_back.red pixel.green = pixel_back.green pixel.blue = pixel_back.blue
Set the RGB values accordingly to “replace” the pixel!
![Page 76: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/76.jpg)
What’s next?
![Page 77: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/77.jpg)
The Console
Data structures
Everyday PythonObject-Oriented Programming
MidtermGraphics
Programming Basics
Roadmap
Life after CS106AP!
Day 1!Images
Images
Advanced Images
![Page 78: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/78.jpg)
Advanced images
● More Photoshop functionality
● Practice with image coordinates
● More complex control flow
● Assignment 2 bluescreen contest
HOMEWORK: Read the SimpleImage reference and install everything!
![Page 79: Images (Part 1) - Stanford University · Images (Part 1) CS106AP Lecture 8. Roadmap Programming Basics The Console Images Data structures Midterm Graphics Object-Oriented Programming](https://reader030.vdocuments.us/reader030/viewer/2022041106/5f0830887e708231d420c8b8/html5/thumbnails/79.jpg)
Advanced images
● More Photoshop functionality
● Practice with image coordinates
● More complex control flow
● Assignment 2 bluescreen contest
This could be YOU!