1 / 94

Introduction to Computing and Programming in Python: A Multimedia Approach

Introduction to Computing and Programming in Python: A Multimedia Approach. Chapter 3: Modifying Pictures using Loops. Chapter Learning Objectives. We perceive light different from how it actually is. Color is continuous Visible light is in the wavelengths between 370 and 730 nanometers

kara
Download Presentation

Introduction to Computing and Programming in Python: A Multimedia Approach

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops

  2. Chapter Learning Objectives

  3. We perceive light different from how it actually is • Color is continuous • Visible light is in the wavelengths between 370 and 730 nanometers • That’s 0.00000037 and 0.00000073 meters • But we perceive light with color sensors that peak around 425 nm (blue), 550 nm (green), and 560 nm (red). • Our brain figures out which color is which by figuring out how much of each kind of sensor is responding

  4. Luminance vs. Color • We perceive borders of things, motion, depth via luminance • Luminance is not the amount of light, but our perception of the amount of light. • We see blue as “darker” than red, even if same amount of light. • Much of our luminance perception is based on comparison to backgrounds, not raw values. Luminance is actually color blind. Completely different part of the brain does luminance vs. color.

  5. Digitizing pictures as bunches of little dots • 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 • Our background/depth acuity is particulary low • Each picture element is referred to as a pixel

  6. Pixels • Pixels are picture elements • Each pixel object knows its color • It also knows where it is in its picture When we zoom the picture to 500%, we can see individual pixels. Week2-lab2

  7. QUESTION • Find <x,y> coordinates of four corners of catapillar.jpg: • Top left corner • Top right corner • Bottom left corner • Bottom right corner

  8. y 0,0 • Answer: x • X:0, Y:0 • X:328, Y:0 • X:0, Y:149 • X:328, Y:149

  9. A Picture is a matrix of pixels • It’s not a continuous line of elements, that is, an array • A picture has two dimensions: Width and Height • We need a two-dimensional array: a matrix • A matrix is a collection of elements arranged in both rows and columns array row matrix column

  10. Referencing a matrix • Each element stores a pixel • We talk about positions in a matrix as (x,y), or (horizontal, vertical) Element[x][y]

  11. QUESTION • What is the value of Element (1,0) in matrix? • What is the value of Element (0,2) in matrix?

  12. Answer: • Element (1,0) is 12 • Element (0,2) is 6

  13. Encoding color • Each pixel encodes color at that position in the picture • Lots of encodings for color • Printers use CMYK: Cyan, Magenta, Yellow, and blacK. • Others use HSB for Hue, Saturation, and Brightness (also called HSV for Hue, Saturation, and Value) • We’ll use the most common for computers • RGB: Red, Green, Blue

  14. RGB Model • we can make up any color by combining red, green, and blue light. • Combining all three gives us pure white. • Turning off all three gives us black.

  15. How does my computer store things in memory? • Bit = Binary Digit • Bit is the smallest unit of information on a machine • A single bit can hold only one of two values: 0 or 1. • Everything on a computer is stored as strings of bits • Byte is composed of 8 consecutive bits

  16. QUESTION • How much information can n bits represent? • n =2 • n=3 • n=4

  17. Answer: • n=2 • Possible patterns (4): • 00, 01, 10, 11 • n=3 • Possible patterns (8): • 000, 001, 010, 011, 100,101,110,111 • n=4 • Possible patterns (16): • 0000, 0001, 0010, 0011, 0100,0101,0110,0111 1000, 1001, 1010, 1011, 1100,1101,1110,1111

  18. Encoding RGB • Each component color (red, green, and blue) is encoded as a single byte (8 bits) • Colors go from (0,0,0) to (255,255,255) • If all three components are the same, the color is in greyscale • E.g. (200,200,200) at (3,1) • BLACK: (0,0,0) • WHITE: (255,255,255) • RED: (255,0,0) • GREEN: (0,255,0) • BLUE: (0,0,255)

  19. How much can we encode in 8 bits? • Let’s walk it through. • If we have one bit, we can represent two patterns: 0 and 1. • If we have two bits, we can represent four patterns: 00, 01, 10, and 11. • If we have three bits, we can represent eight patterns: 000, 001, 010, 011, 100, 101, 110, 111 • General rule: In n bits, we can have 2n patterns • In 8 bits, we can have 28 patterns, or 256 • If we make one pattern 0, then the highest value we can represent is 28-1, or 255

  20. Is that enough? • We’re representing color in 24 (3 * 8) bits. • That’s 16,777,216 (224) possible colors • Our eye can discern millions of colors, so it’s probably pretty close • But the real limitation is the physical devices: We don’t get 16 million colors out of a monitor • Some graphics systems support 32 bits per pixel • May be more pixels for color, or an additional 8 bits to represent 256 levels of translucence

  21. Size of images

  22. Reminder: Manipulating 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 >>> print getWidth(myPict) 222 >>> print getHeight(myPict) 294

  23. What’s a “picture”? • An encoding that represents an image • Knows its height and width • Knows its filename • Knows its window if it’s opened (via show and repainted with repaint)

  24. Manipulating pixels >>> pixel=getPixel(picture,1,1) >>> print pixel Pixel, color=color r=168 g=131 b=105 >>> pixels=getPixels(picture) >>> print pixels[0] Pixel, color=color r=168 g=131 b=105 getPixel(picture,x,y) gets a single pixel at coordinates (x,y) . getPixels(picture) gets all of them in an array. (Square brackets is a standard array reference notation—which we’ll generally not use.)

  25. What can we do with a pixel? • Pixels know where they come from • Function getX: returns the x coordinate of pixel • Function getY: returns the y coordinate of pixel

  26. Each pixel knows how to getRed and setRed (Green and blue work similarly) >>> print getRed(pixel) 2 >>> setRed(pixel, 255) >>> print getRed(pixel) 255

  27. What can we do with a pixel? • getRed, getGreen, and getBlue are functions that take a pixel as input and return a value between 0 and 255 • setRed, setGreen, and setBlue are functions that take a pixel as input and a value between 0 and 255

  28. We can also get, set, and make Colors • getColor takes a pixel as input and returns a Color object with the color at that pixel • setColor takes a pixel as input and a Color, then sets the pixel to that color >>> color=getColor(pixel) >>> print color color r=255 g=131 b=105 >>> setColor(pixel,color)

  29. makeColor takes red, green, and blue values (in that order) between 0 and 255, and returns a Color object >>> newColor=makeColor(0,100,0) >>> print newColor color r=0 g=100 b=0

  30. pickAColor lets you use a color chooser and returns the chosen color >>> newcolor=pickAColor() >>> print newcolor color r=255 g=51 b=51

  31. How do you find out what RGB values you have? And where? Method1: Use explore function

  32. How do you find out what RGB values you have? And where? • Method2: Use the MediaTools! The MediaTools menu knows what variables you have in the Command Area that contain pictures

  33. Distance between colors? • Sometimes you need to, e.g., when deciding if something is a “close enough” match • How do we measure distance? • Pretend it’s cartesian coordinate system • Distance between two points: • Distance between two colors:

  34. distance function prints the distance between two colors: >>> color1 = makeColor(0,100,0) >>> color2 = makeColor(120,230,23) >>> print distance(color1,color2) 178.4068384339569

  35. Demonstrating: Manipulating Colors >>> print color color r=81 g=63 b=51 >>> print newcolor color r=255 g=51 b=51 >>> print distance(color,newcolor) 174.41330224498358 >>> print color color r=168 g=131 b=105 >>> print makeDarker(color) color r=117 g=91 b=73 >>> print color color r=117 g=91 b=73 >>> newcolor=pickAColor() >>> print newcolor color r=255 g=51 b=51 >>> print getRed(pixel) 168 >>> setRed(pixel,255) >>> print getRed(pixel) 255 >>> color=getColor(pixel) >>> print color color r=255 g=131 b=105 >>> setColor(pixel,color) >>> newColor=makeColor(0,100,0) >>> print newColor color r=0 g=100 b=0 >>> setColor(pixel,newColor) >>> print getColor(pixel) color r=0 g=100 b=0

  36. QUESTION • Write a program RepaintEx1.py to change color of following pixels to yellow in barbara.jpg: • (10,100) • (11,100) • (12,100) • (13,100) • Hint: Use pickAColor() function to find the RGB values of yellow

  37. Answer: file="f:/mediasources/barbara.jpg" pict=makePicture(file) show(pict) yel= makeColor(255, 255, 0) setColor(getPixel(pict,10,100),yel) setColor(getPixel(pict,11,100),yel) setColor(getPixel(pict,12,100),yel) setColor(getPixel(pict,13,100),yel)

  38. Seeing changes in the picture • If you show your picture and then change pixels, your picture is not automatically updated! • After manipulating picture calling repaint will update the picture: >>> file="/Users/guzdial/mediasources/barbara.jpg" >>> pict=makePicture(file) >>> show(pict) >>> setColor(getPixel(pict,10,100),yellow) >>> setColor(getPixel(pict,11,100),yellow) >>> setColor(getPixel(pict,12,100),yellow) >>> setColor(getPixel(pict,13,100),yellow) >>> repaint(pict)

  39. Color Constants

  40. Question: How to decrease the red by 50% in a given picture? From what we learnt so far, we can write a code like this: >>> file = “c:/ip-book/mediasources/barbara.jpg” >>> pict = makePicture(file) >>> show (pict) >>> pixels = getPixels(pict) >>> setRed(pixels[0], getRed(pixels[0]) * 0.5) >>> setRed(pixels[1], getRed(pixels[1]) * 0.5) >>> setRed(pixels[2], getRed(pixels[2]) * 0.5) >>> setRed(pixels[3], getRed(pixels[3]) * 0.5) >>> setRed(pixels[4], getRed(pixels[4]) * 0.5) >>> setRed(pixels[5], getRed(pixels[5]) * 0.5) >>> repaint(pict) But that’s really dull and boring to change each pixel at a time…Isn’t there a better way?

  41. Use a loop!Our first picture recipe def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Used like this: >>> file="/Users/guzdial/mediasources/barbara.jpg" >>> picture=makePicture(file) >>> show(picture) >>> decreaseRed(picture) >>> repaint(picture)

  42. Our first picture recipe works for any picture def decreaseRed( picture ): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Used like this: >>> file="/Users/guzdial/mediasources/katie.jpg" >>> picture=makePicture(file) >>> show(picture) >>> decreaseRed(picture) >>> repaint(picture)

  43. Use a for loop!Our first picture recipe def decreaseRed(pict): allPixels = getPixels(pict) for p in allPixels: value = getRed(p) setRed(p, value * 0.5) The loop - Note the indentation!

  44. How for loops are written defdecreaseRed(pict): allPixels = getPixels(pict) for p in allPixels: value = getRed(p) setRed(p, value * 0.5) • foris the name of the command • An index variable is used to hold each of the different values of a sequence (p in this example) • The word in • A 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)

  45. What happens when a for loop is executed • The index variable is set to an item in the sequence • The block is executed • Thevariable 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.

  46. getPixels returns a sequence of pixels • Each pixel knows its color and place in the original picture • Change the pixel, you change the picture • So the loops here assign the index variable p to each pixel in the picturepicture, one at a time. def decreaseRed(picture): allPixels = getPixels(picture) for p in allPixels originalRed = getRed(p) setRed(p, originalRed * 0.5) or equivalently… sequence def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5)

  47. Do we need the variable originalRed? • No: Having removed allPixels, we can also do without originalRed in the same way: • We can calculate the original red amount right when we are ready to change it. • It’s a matter of programming style. The meanings are the same. def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed* 0.5) def decreaseRed(picture): for p in getPixels(picture): setRed(p, getRed(p) * 0.5)

  48. Let’s walk that through slowly… Here we take a picture object in as a parameter to the function and call it picture defdecreaseRed(picture): for p ingetPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) picture

  49. Now, get the pixels We get all the pixels from the picture, then make p be the name of each one one at a time def decreaseRed(picture): for p ingetPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) picture getPixels() Pixel, color r=135 g=131b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … p

More Related