Professional Documents
Culture Documents
Luminance is actually
colour blind. Completely
different part of the brain
does luminance vs. colour.
44
Pixels
Referencing a matrix
We talk about positions
in a matrix as (x,y), or
(horizontal, vertical) or
(column, row)
Element (1,0) in the
matrix at left is the
value 12
Element (0,2) is 6
Encoding colour
Each pixel encodes colour at that position in the
picture
Lots of encodings for colour
Printers use CMYK: Cyan, Magenta, Yellow, and blacK.
Others use HSB for Hue, Saturation, and Brightness (also called HSV
for Hue, Saturation, and Value)
10
11
Encoding RGB
Each component
colour (red, green, and
blue) is encoded as a
single byte
Colours go from (0,0,0)
to (255,255,255)
(200,200,200) at (3,1)
12
Is that enough?
Were representing colour in 24= 3 8 .
Thats 16,777,216 (224) possible colours
Our eye can discern millions of colours, so its probably
pretty close
But the real limitation is the physical devices: We dont
get 16 million colour shades out of a monitor
13
Size of images
320 x 240
image
640 x 480
image
24 bit colour
230,400
bytes
32 bit colour
307,200
bytes
1,228,800
bytes
1024 x 768
monitor
3,145,728
bytes
14
>>> 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
15
Whats a picture?
An encoding that represents an image
Knows its height and width (size)
Knows its filename (name)
Knows its window if its opened (via show and repainted
with repaint)
16
Manipulating pixels
getPixel(picture,x,y) gets a single pixel.
getPixels(picture) gets all of pixels in an
array. (Square brackets is a standard array reference
notationwhich well generally not use.)
>>> pixel=getPixel(picture,1,1)
>>> print pixel
Pixel, colour=colour r=168 g=131 b=105
>>> pixels=getPixels(picture)
>>> print pixels[0]
Pixel, colour=colour r=168 g=131 b=105
17
Pictorially
pixels [0]
pixels [1]
pixels [2]
pixels [3]
pixels [n]
Where n is the
number of pixels in
the picture
18
QuickTime and a
H.264 decompressor
are needed to see this picture.
19
20
21
makePicture(fileName) or makeEmptyPicture().
Run the openPictureTool(Picture) or select
'Picture Tool...' from the MediaTools menu
I havent been able to do it from the MediaTools menu.
If you can, tell me how!
22
QuickTime and a
decompressor
are needed to see this picture.
23
24
25
>>> file="/Users/guzdial/mediasources/barbara.jpg"
>>> pict=makePicture(file)
>>> show(pict)
>>> setColour(getPixel(pict,10,100),yellow)
>>> setColour(getPixel(pict,11,100),yellow)
>>> setColour(getPixel(pict,12,100),yellow)
>>> setColour(getPixel(pict,13,100),yellow)
>>> repaint(pict)
26
Use a loop!
Our first picture recipe
def decreaseRed(picture):
for p in getPixels(picture):
value=getRed(p)
setRed(p,value*0.5)
27
def decreaseRed(picture):
for p in getPixels(picture):
value=getRed(p)
setRed(p,value*0.5)
28
29
30
def decreaseRed(pict):
allPixels = getPixels(pict)
for p in allPixels:
value = getRed(p)
setRed(p, value * 0.5)
31
32
33
def decreaseRed(picture):
allPixels = getPixels(picture)
for p in allPixels
originalRed = getRed(p)
setRed(p, originalRed * 0.5)
or equivalently
def decreaseRed(picture):
for p in getPixels(picture):
originalRed = getRed(p)
setRed(p,originalRed * 0.5)
34
35
picture
36
Pixel,
colour
r=135
g=131
b=105
Pixel,
colour
r=133
g=114
b=46
Pixel,
colour
r=134
g=114
b=45
getPixels()
p
School of Computer Science & IT
37
Pixel,
colour
r=135
g=131
b=105
Pixel,
colour
r=133
g=114
b=46
p
School of Computer Science & IT
Pixel,
colour
r=134
g=114
b=45
getPixels()
originalRed= 135
38
Pixel,
colour
r=133
g=114
b=46
p
School of Computer Science & IT
Pixel,
colour
r=134
g=114
b=45
getPixels()
picture
originalRed = 135
39
picture
Pixel,
colour
r=67
g=131
b=105
Pixel,
colour
r=133
g=114
b=46
p
Pixel,
colour
r=134
g=114
b=45
getPixels()
originalRed = 135
40
def decreaseRed(picture):
for p in getPixels(picture):
originalRed = getRed(p)
setRed(p, originalRed * 0.5)
picture
Pixel,
colour
r=67
g=131
b=105
Pixel,
colour
r=133
g=114
b=46
pp
Pixel,
colour
r=134
g=114
b=45
getPixels()
originalRed = 133
41
Pixel,
colour
r=67
g=131
b=105
Pixel,
colour
r=66
g=114
b=46
pp
Pixel,
colour
r=134
g=114
b=45
getPixels()
value = 133
42
to this!
43
44
45
def decreaseRed(picture):
for p in getPixels(picture):
value=getRed(p)
setRed(p,value*0.5)
46
Read it as a Recipe
def decreaseRed(pict):
for p in getPixels(pict):
value=getRed(p)
setRed(p,value*0.5)
47
48
49
50
Increasing Red
def increaseRed(picture):
for p in getPixels(picture):
value=getRed(p)
setRed(p,value*1.2)
51
52
Clearing Blue
def clearBlue(picture):
for p in getPixels(picture):
setBlue(p,0)
53
54
A Sunset-generation Function
def makeSunset(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p,value*0.7)
value=getGreen(p)
setGreen(p,value*0.7)
55
def darken(picture):
for px in getPixels(picture):
colour = getColour(px)
colour = getColour(px)
colour = makeLighter(colour)
colour = makeDarker(colour)
setColour(px ,colour)
setColour(px ,colour)
for px in getPixels(picture):
56
Creating a negative
Lets think it through
R,G,B go from 0 to 255
Lets say Red is 10. Thats very light red.
Whats the opposite? LOTS of Red!
The negative of that would be 245: 255-10
57
58
Programming Style
The constant 255 is called a magic number
We know what it means, but what about someone
else?
# MAX_COLOUR is the maximum integer we can represent in a byte
MAX_COLOUR = 255
def negative(picture):
for px in getPixels(picture):
red=getRed(px)
green=getGreen(px)
blue=getBlue(px)
setColour(px,negColour)
59
60
Converting to greyscale
We know that if red=green=blue, we get grey
61
Converting to greyscale
def greyScale(picture):
for p in getPixels(picture):
intensity = (getRed(p)+getGreen(p)+getBlue(p))/3
setColour(p,makeColour(intensity,intensity,intensity))
62
63
64
65
66
67
68
reduceBlue(picture)
reduceGreen(picture)
def reduceBlue(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p,value *0.7)
def reduceGreen(picture):
for p in getPixels(picture):
value=getGreen(p)
setGreen(p,value *0.7)
70
71
def makeSunset(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p,value*0.7)
value=getGreen(p)
setGreen(p,value*0.7)
def makeSunset(picture):
reduceBlue(picture)
reduceGreen(picture)
def reduceBlue(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p,value*0.7)
def reduceGreen(picture):
for p in getPixels(picture):
value=getGreen(p)
setGreen(p,value*0.7)
72
def makeSunset(picture):
reduceBlue(picture)
reduceGreen(picture)
def reduceBlue(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p,value*0.7)
def reduceGreen(picture):
for p in getPixels(picture):
value=getGreen(p)
setGreen(p,value*0.7)
73
Considering variations
We can only do this because
reduceBlue and
reduceGreen, do one and
only one thing.
def makeSunset(picture):
reduceBlue(picture)
reduceGreen(picture)
def reduceBlue(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p,value*0.7)
def reduceGreen(picture):
for p in getPixels(picture):
value=getGreen(p)
setGreen(p,value*0.7)
74
75
>>> makeSunset(picture)
>>> show(picture)
def makeSunset(picture):
reduceBlue(picture)
reduceGreen(picture)
def reduceBlue(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p,value*0.7)
def reduceGreen(picture):
for p in getPixels(picture):
value=getGreen(p)
setGreen(p,value*0.7)
76
getName()
print(Hello, name)
def getName():
>>> main()
School of Computer Science & IT
77
melbourne()
jerusalem()
def melbourne():
birds = 5000
def jerusalem():
birds = 8000
>>>
>>> main()
78
Passing arguments
def main():
value = 5
showDouble(value)
def showDouble(number):
result = number * 2
print(result)
>>>
>>> main()
79
value = 5
showDouble(value)
def showDouble(value):
result = value * 2
print(value)
>>>
>>> main()
80
Passing arguments
def main():
value = 5
showDouble(value)
print value
def showDouble(value):
result = value * 2
print(value)
>>>
>>> main()
School of Computer Science & IT
81
Side effects?
def main():
value = 5
showDouble(value)
print value
def showDouble(number):
number = number * 2
print(number)
>>>
>>> main()
82
value = 5
showDouble(value)
print value
def showDouble(value):
value = value * 2
print(value)
>>>
>>> main()
83
Global Variables
These are to be used VERY sparingly
You need to have a good reason to have this
They are variables which are available to all
functions
84
def show_value():
print(my_value)
>>>
>>> show_value()
85
def show_value():
my_value = my_value + 1
print(my_value)
>>>
>>> show_value()
School of Computer Science & IT
86
Resultant error
>>> ======= Loading Progam =======
>>> show_value()
The error was: 'my_value'
Local name referenced but not bound to a value.
A local name was used before it was created.
You need to define the method or variable before
you try to use it.
Please check line 3 of /Users/isaac/Desktop/try
School of Computer Science & IT
87
global my_value
my_value = my_value + 1
print(my_value)
>>>
>>> show_value()
It prints 11
88
89
90
91
92
def decreaseRed(picture):
for p in getPixels(picture):
value=getRed( p)
setRed(p,value*0.5)
93
94
95
96
def clearBlue(picture):
for p in getPixels(picture):
setBlue(p,0)
def clearBlue(picture):
for p in getPixels(picture):
value = getBlue(p)
setBlue(p,value*0)
97
98