Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
Compute with Images¶
Pictures on a computer are broken up into little bits called pixels, for picture (pix) elements (els). These are laid out on a grid, from left to right (the horizontal or x dimension) and top to bottom (the vertical or y dimension).
Pixels are quite small. Even this small picture below has 180 columns and 240 rows of pixels:
Each pixel has a color associated with it: An amount of red, an amount of green, and an amount of blue. The amount can be in the range of 0 to 255 where 0 is none of that color and 255 is the maximum amount of that color. A pixel is displayed using light, not paint, so it may work a bit differently than you might expect if you only have experience making colors by mixing paint. For example, you would mix blue and yellow paint to make green, but you mix red and green light to make yellow light. See http://www.webexhibits.org/causesofcolor/1BE.html for a procedure to try this out for yourself.
All image manipulations in Photoshop and all filters in Instagram or Hipstamatic are created through manipulating those red, green, and blue color components in each pixel.
Let’s remove the red from this picture. The program below does that.
There are lot of lines in the program below. Don’t worry if they don’t all make sense to you right now.
Especially when we write programs to manipulate images, you can ignore many of the lines. Some read in a library to allow us to work with images, like
from image import *. Others like
win = ImageWin(img.getWidth(),img.getHeight())and
img.draw(win)let us see the result.
Words after the
#are ignored by the computer. They are comments to human readers to help them understand a program.
The lines that are important are under the comments (lines that start with a
#). Press the button to hear an audio explanation of the important lines. Press the button to run the program and show the changed image. Please note that processing all those pixels can take a few minutes.