ch04 - Chapter 4 Modifying Pixels in a Range 1 Chapter...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 4: Modifying Pixels in a Range 1 Chapter Learning Goals 2 Reminder: Pixels are in a matrix Matrices have two dimensions: A height and a width We can reference any element in the matrix with (x,y) We refer to those coordinates as index numbers or indices We sometimes want to know where a pixel is, and getPixels doesn’t let us know that. 3 Pixels in a Matrix “Barbara.jpg” has height 293 (bottommost index is 292) and width 221 (rightmost index is 220) 4 Introducing the function range Range is a function that returns a sequence between its first two inputs, possibly using a third input as the increment >>> print range(1,4) [1, 2, 3] >>> print range(-1,3) [-1, 0, 1, 2] >>> print range(1,10,2) [1, 3, 5, 7, 9] >>> print range(3) [0,1,2] Notice: • The end value is never included. • range(0,10) ends at 9. • If you leave out a start value, it’s assumed to be zero. 5 Side Note: That thing in is a sequence >>> a=[1,2,3] >>> print a [1, 2, 3] >>> a = a + 4 An attempt was made to call a function with a parameter of an invalid type >>> a = a + [4] >>> print a [1, 2, 3, 4] >>> a[0] 1 We can assign names to sequences, print them, add items to sequences, and access individual pieces of them. We can also use for loops to process each element of a sequence. 6 We can use range to generate index numbers We’ll do this by working the range from 0 to the height-1, and 0 to the width-1. Using the range function will make it easy to start from 0 and stop before the end value. But we’ll need more than one loop. Each for loop can only change one variable, and we need two for indexing a matrix 7 Working the pixels by number We will use range , along with nested loops One to walk the width, the other to walk the height Be sure to watch your blocks (i.e., indentation) carefully! def increaseRed2(picture): for x in range(0,getWidth(picture)): for y in range(0,getHeight(picture)): px = getPixel(picture,x,y) value = getRed(px) setRed(px,value*1.1) 8 Be careful with the getPixel function getPixel(picture,x,y) Notice that the getPixel function has three parameters The first is the name of the picture from which you want a pixel color The second is the x (horizontal, i.e., across the width) index (i.e. it says the column of the matrix) The third is the y (vertical, i.e., down the height) index (i.e. it says the row of the matrix) So, for Barbara.jpg (width 221 and height 293) the upper left pixel is getPixel(picture,0,0) the bottom right pixel is getPixel(picture,220,293) 9 Image manipulations Now you understand how to manipulate a pixel if you know where the pixels are you can do a lot of things to pictures: Mirror an image Rotate an image Copying images (you can create collages!) Crop an image Increase or decrease the size of an image (scale up or down) Blur an image Useful stuff!...
View Full Document

{[ snackBarMessage ]}

Page1 / 59

ch04 - Chapter 4 Modifying Pixels in a Range 1 Chapter...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online