Phyton-Working With Pixels
Phyton-Working With Pixels
Reminder: Conditionals
if age < 18:
showInformation(“Sorry, not allowed to vote yet.”)
else:
showInformation(“Please select candidate.”)
Relational Operators
• a>b
• a<b
• a == b (equality)
• a <= b (“less than or equal”)
• a >= b (“greater than or equal”)
• != (inequality)
Note: The order of signs matters: =! =< => do not work!
There should be no spaces inside the relational operator, so “a < = b” also
does not work (See also Appendix A.4, p362)
Getting the leading zero into
the filename:
if n < 10:
filename = “swatch0” + str(n) + “.jpg”
else:
filename = “swatch” + str(n) + “.jpg”
Getting the leading zero into
the filename:
if n < 10:
filename = “swatch0” + str(n) + “.jpg”
else: # here we know n >= 10
filename = “swatch” + str(n) + “.jpg”
x = 12
>>> p = getPixel(picture,12,9)
>>> print p
Pixel, red=108 green=86 blue=142
What can we do with a
pixel p?
• getRed(p), functions that take
a pixel (p) as input
• getGreen(p) and return a value
• getBlue(p) between 0 and 255
x = 12
>>> pixel=getPixel(picture,12,9)
>>> print pixel
Pixel, red=108 green=86 blue=142
>>> value = getRed(pixel)
>>> setRed (pixel, value+50)
>>> setGreen(pixel, 0)
>>> setBlue(pixel, getBlue(pixel)/2)
red=108
red=158 green=86
Green=0 Blue=121
blue=142
y=9
Color:(108,86,142)
Color:(158,0,121)
Position: (12,9)
x = 12
>>> pixel=getPixel(picture,12,9)
>>> print pixel
Pixel, red=108 green=86 blue=142
>>> value = getRed(pixel)
>>> setRed (pixel, value+50)
>>> setGreen(pixel, 0)
>>> setBlue(pixel, getBlue(pixel)/2)
red=108 green=86 blue=142
y=9
Color:(108,86,142)
Position: (12,9)
x = 12
>>> pixel=getPixel(picture,12,9)
>>> print pixel
Pixel, red=108 green=86 blue=142
>>> redValue = getRed(pixel)
>>> greenValue = getGreen(pixel)
>>> blueValue = getBlue(pixel)
>>> newColor = makeColor(redValue+50, 0, getBlue(pixel)/2)
>>> setColor(pixel, newColor)
red=158 Green=0 Blue=121
y=9
Color:(158,0,121)
Position: (12,9)
x = 12
>>> pixel=getPixel(picture,12,9)
>>> print pixel
Pixel, red=108 green=86 blue=142
>>> redValue = getRed(pixel)
>>> greenValue = getGreen(pixel)
>>> blueValue = getBlue(pixel)
>>> newColor = makeColor(redValue+50, 0, getBlue(pixel)/2)
>>> setColor(pixel, newColor)
Repeating an action for all the
pixels in a picture
Example:
for p in getPixels(picture):
value = getRed(p)
setRed(p, value*0.5)
Repeating an action for all the
pixels in a picture
decreaseRed()
Example:
def decreaseRed(picture):
for p in getPixels(picture):
value = getRed(p)
setRed(p, value*0.5)
More examples:
More examples (link) - you can copy and
paste these to save time
• decreaseGreen()
• decreaseBlue()
• clearBlue()
• lighten()
• darken()
• negative()
• grayScale()
18
“Posterize” function
• For each pixel, if
red<128, we set red=0,
otherwise we set red=255
• Similarly for green, blue
Assignment: Create a python
function for each of the following:
1. Decrease red by 20%
2. Decrease green by 20%
3. Decrease blue by 20%
4. Increase red by 20%, if possible (i.e., if it does not
exceed 255)
5. Similarly for increasing blue and green
6. “Posterize”
7. Think of another way to change an image and
implement a function to do that
20