Software Carpentry logo

Image Processing

April 24, 2010: We are pleased to announce that Version 4 of this course is now under development. For updates and an early peek at the content, please check out the Software Carpentry blog at http://www.software-carpentry.org/blog/.

1) Introduction

2) You Can Skip This Lecture If...

3) Pictures and Pixels

Pixels in a Picture

Figure 8.1: Pixels in a Picture

4) Color Representation

Color Combinations

Figure 8.2: Color Combinations

5) PyMedia Introduction

media.create_picture(width, height, color)
Create a width by height color picture
media.choose_file()
opens a file dialog and lets the user select a file
media.load_picture(filename)
tells Python to load the picture from file named filename

6) PyMedia: Pictures

media.show(pic)
makes the picture appear on the screen
media.get_width(pic)
Retrieve the width of the picture
media.get_height(pic)
Retrieve the height of the picture

7) PyMedia: Pixels and Colors

pixel = media.get_pixel(pic, x, y)
Retrieve pixel (x, y) from Picture pic.
c = media.get_color(pixel)
Retrieve the color of a pixel
media.set_color(pixel, c)
Set the color of a pixel to c
media.get_green(pixel)
Retrieve the green value (an int) of a pixel
media.set_green(pixel, g)
Set the green value of a pixel to int g
c = media.create_color(r, g, b)
Create color (r, g, b)

8) PyMedia: Iterating over Pixels

import media
import color

pic = media.create_picture(100, 200, media.black)

for i in range(media.get_width(pic)):
  for j in range(media.get_height(pic)):
  pixel = media.get_pixel(pic, i, j)
  media.set_color(pixel,
    media.create_color(i % 255, j % 255, 0))

pic.show()

9) Task: Cheesy Daytime to Sunset

Niagara During the Day

Figure 8.3: Niagara During the Day

Niagara at Sunset

Figure 8.4: Niagara at Sunset

10) A Sunset Algorithm

For each pixel in pic:
  get the blue component of the pixel
  reduce that number by 30%
  set the blue component of the pixel to the reduced number

  get the green component of the pixel
  reduce that number by 30%
  set the green component of the pixel to the reduced number

11) Task: Send Greg Around the World

Greg

Figure 8.5: Greg Before Travelling

12) Lake Elmore

Lake Elmore

Figure 8.6: Lake Elmore, Vermont

13) India Tea Plantation

India Tea Plantation

Figure 8.7: India Tea Plantation, Kerala Province

14) Giraffes in Africa

Giraffes

Figure 8.8: Giraffes in Africa

15) Giant Mosquito

Giant Mosquito

Figure 8.9: Giant Mosquito