Title: Python: Digging into the pixels
1Python Digging into the pixels
2Review
- JES Functions for Files, Input/Output, Pictures,
Colors - Using makeColor() to create colors
- making color swatches and saving them to .jpg
files - Repetition for loops
- for number in range(0,N) writePictureTo(swatch,
"swatch" str(number) ".jpg") setAllPixelsToAC
olor(swatch, currentColor) currentColor
makeColor(20,50, number20) - Conditional if/else
- if number lt 10
- filename "swatch0" str(number) ".jpg"
- else
- filename "swatch" str(number) ".jpg"
- lists
- 3, 8, 5
- range(0,200)
- range(4,7)
3Manipulating pixels
getPixel(picture,x,y) gets a single
pixel. getPixels(picture) gets all of them in an
array
4red108
green86
blue142
y 9
Color(108,86,142) Position (12,9)
x 12
- gtgtgt pixelgetPixel(picture,12,9)
- gtgtgt print pixel
- Pixel, red108 green86 blue142
- gtgtgt allThePixelsgetPixels(picture)
- gtgtgt print allThePixels0
- Pixel red191 green149 blue120
5What 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
6We can also get, set, and modify 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 - We also have functions that can makeLighter() and
makeDarker() an input color - Last time we saw that we can also create colors
- makeColor() takes red, green, and blue values (in
that order) between 0 and 255, and returns a
Color object - pickAColor() lets you use a color chooser and
returns the chosen color
7Demonstrating Manipulating Colors
gtgtgt print getRed(pixel) 168 gtgtgt
setRed(pixel,255) gtgtgt print getRed(pixel) 255 gtgtgt
colorgetColor(pixel) gtgtgt print color color r255
g131 b105 gtgtgt setColor(pixel,color) gtgtgt
newColormakeColor(0,100,0) gtgtgt print
newColor color r0 g100 b0 gtgtgt
setColor(pixel,newColor) gtgtgt print
getColor(pixel) color r0 g100 b0
gtgtgt print color color r81 g63 b51 gtgtgt print
newcolor color r255 g51 b51 gtgtgt print
color color r168 g131 b105 gtgtgt print
makeDarker(color) color r117 g91 b73 gtgtgt print
color color r117 g91 b73 gtgtgt
newcolorpickAColor() gtgtgt print newcolor color
r255 g51 b51
8We can change pixels directly
gtgtgt file picKAFile() gtgtgt pictmakePicture(file) gt
gtgt show(pict) gtgtgt setColor(getPixel(pict,10,100),y
ellow) gtgtgt setColor(getPixel(pict,11,100),yellow)
gtgtgt setColor(getPixel(pict,12,100),yellow) gtgtgt
setColor(getPixel(pict,13,100),yellow) gtgtgt
repaint(pict)
9Media Tools
How do you find out what RGB values you have? And
where?
JES Picture function OR Media Tools menu
10Assignment
- Create a menu-driven picture manipulation program
that allows the user to make various changes to
images. Here is the algorithm - welcome the user to your program
- get the user to select a picture to work on
- show(picture)
- ask the user to select an option from a list of
numbers - reduce red
- reduce green
- reduce blue
- posterize
- grayscale
- lta feature of your choicegt
- repaint(picture)
- ask user whether to save the picture work, and if
the response is yes, save it - show goodbye/thank you message
11Iterating through all the pixels
- Example
- for p in getPixels(picture) value
getRed(p) setRed(p, value0.5)