Download presentation
Presentation is loading. Please wait.
1
CS 177 Week 3 Recitation Slides
Pictures, Pixels, Colors and for Loop Look at 1-fridayMix slides
2
Announcements
3
ANY QUESTIONS?
4
Reminder: Why digitize media?
Real media is analogue (continuous). To digitize it, we break it into parts and encode them into numbers. By encoding them, we can more easily manipulate them, store them, transmit them without error, etc. 4
5
Digitization of Pictures
We digitize pictures into lots of little dots Enough dots and it looks like a continuous whole to our eye Our eye has limited resolution Each little dot (picture element) is referred to as a pixel 5
6
We already saw how to display a picture in JES
myFile = pickAFile() myPicture = makePicture(myFile) show(myPicture)
7
Now, let’s see how a picture is represented in JES
A Picture is a matrix of pixels Row: width Column: height Pixels are picture elements Each pixel object knows its color It also knows where it is in its picture
8
Encoding Colors with RGB Model
Each pixel encodes the color at that position in the picture How we represent a color? We use RGB model to encode colors in computers. RGB: Red, Green, Blue.
9
RGB Color Model Each component color (red, green, and blue) is encoded as a single byte Colors go from (0,0,0) black to (255,255,255) white
10
Encoding RGB: Bits and Patterns
General rule: In n bits, we can have 2n patterns In 8 bits, we can have 28 patterns, or 256 If we make the first pattern 0, then the highest value we can represent is 28-1, or 255 We’re representing color in 24 (3 * 8) bits.
11
Reminder: how to show pictures
>>> file=pickAFile() >>> print file /Users/guzdial/mediasources/barbara.jpg >>> picture=makePicture(file) >>> show(picture) >>> print picture Picture, filename /Users/guzdial/mediasources/barbara.jpg height 294 width 222 Picture is a Python (JES) object Picture is a matrix of 294 rows x 222 colums Each element of a matrix contains a pxel
12
Exploring the pixels of a picture..
You can use the picture tool of JES to see the color of a picture pixel….
13
Manipulating Pixels or you can use a Python command to:
get a single pixel at given x,y position getPixel(picture,x,y) get all the pixels into an array getPixels(picture) How to do it STEP 1: write your function in the JES edit area 13 13
14
What can we do with a pixel?
Once you got a pixel, you can get its color components: getRed, getGreen, and getBlue Input: a pixel Output: a value between 0 and 255 Or you can change one of its color components: setRed, setGreen, and setBlue Step 2: save your function in a file
15
What can we do with a color?
getColor: Input: a pixel Output: a Color object with the color at that pixel setColor: Input: a pixel and a Color Output: a pixel with the color set makeColor: Input: red, green, and blue values (in that order) between 0 and 255 Output: a Color object pickAColor lets you use a color chooser and returns the chosen color We also have functions that can makeLighter and makeDarker an input color Step 2: save your function in a file 15 15
16
Examples: Manipulating Colors
Let’s see some examples… Demo You must give a name to your file
17
Change Pixels Directly …
>>> setColor(getPixel(pict,10,100),yellow) >>> setColor(getPixel(pict,11,100),yellow) >>> setColor(getPixel(pict,12,100),yellow) >>> setColor(getPixel(pict,13,100),yellow) >>> … What if we want to change millions of pixels? Better way? … Use a loop. Step 3: load your function in main memory so as JES can execute it
18
Example of a for Loop def decreaseRed(picture):
for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Let’s see an example… Demo
19
Rules of for Loops Command name: for
An index variable p is used to hold each of the different values of a sequence The word in A Python function that generates a sequence The index variable will be the name for one value in the sequence, each time through the loop A colon (“:”) And a block (the indented lines of code)
20
How a for Loop Works The index variable is set to an item in the sequence The block is executed The variable is often used inside the block Then execution loops to the for statement, where the index variable gets set to the next item in the sequence Repeat until every value in the sequence was used.
21
Using the loop in our Egg Omelette recipe …
One Egg Omelette recipe: Put oil in pan Pre-heat pan Crack an egg into bowl Add Salt Wisk the contents of bowl Pour contents of bowl into pan Wait for 3 minutes
22
Using the loop in our Egg Omelette recipe…
Four Egg Omelettes recipe: Put oil in pan Pre-heat pan Crack an egg into bowl Add Salt Wisk the contents of bowl Pour contents of bowl into pan Wait for 3 minutes Repeat the above procedure again for the second egg omelette Repeat the above procedure again for the third egg omelette Repeat the above procedure again for the fourth egg omelette
23
Final QUESTIONS???
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.