Office Hour

I have shifted my office hour from Wednesday to Monday; same time (1:00 - 2:00)

In general, you can find me on campus Monday, Tuesday, & Thursday

I'm reachable by e-mail most any time




Last Week's Exercise

An example implementation: color.py

Some comments:






Vector vs Raster

Historically, displays have been divided between vector and raster

vector - pictures are drawn as a set of precise lines, connecting arbitrary points on the screen



raster - pictures are drawn by scanning the screen in a discrete sequence of rows




Vector vs Raster

A similar distinction continues - graphical objects can be described by images or geometry

    





Images

     

A digital image is a 2 dimensional array of pixel colors

Pixel = "picture element"

Each pixel is a sample of a continuous, analog image






Pixels



Pointillism can be considered to take a similar approach - breaking an image down into discrete samples






Image Data

Basic image data in computer memory is a stream of numbers


1 239 120 1 1 37 94 8 92 31 80 92 134 89 2 3 50 9 3 10 93 109 134  ...

Important additional information is:


Total memory needed for a simple image:

    width * height * components * bytes_per_component

e.g. a 512x512, RGB, 8-bit image requires 768 kilobytes






Image Files

Image data is often saved in or loaded from a file

There are many common image formats: JPEG, TIFF, PNG, GIF, etc

Features which may vary between formats are:






PIL

Provides support for reading, writing, and manipulating images in several formats.

PIL website: www.pythonware.com/products/pil/
Handbook: www.pythonware.com/library/pil/handbook/


PIL is installed on all the PCs in rooms 242 and 265

The PIL installer for Windows can be grabbed from my Python installation page.
It's also included in the 'kitchensink' distribution for Mac OS/X






PIL Example

>>> import Image
>>> img = Image.open("foo.jpg")
>>> img.size
(512, 512)
>>> img.mode
'RGB'
>>> newimg = img.resize((32,32))
>>> newimg.save("bar.tif")

Note: don't do "from Image import *", because that will replace the normal open() function with the Image package's open().






PIL Functions

open

img = Image.open('myimage.tif')

new

img = Image.new('RGB', (640,480), (255, 255, 255))

save

img.save('newimage.jpg')






PIL Functions

resize

img2 = img1.resize( (256, 256), Image.ANTIALIAS )

transpose

img2 = img1.transpose( Image.FLIP_TOP_BOTTOM )

rotate

img2 = img1.rotate(30)

convert

img2 = img1.convert( 'L' )






PIL Functions

tostring

imgdata = img.tostring()

fromstring

img.fromstring(imgdata)
    or
img = Image.fromstring('RGB', (640, 480), imgdata)

getpixel

rgb = img.getpixel( (10, 31) )

putpixel

img.putpixel( (10, 31), (255, 255, 0) )






OpenGL Functions

GL function names follow a standardized scheme


All begin with "gl", followed by the basic name of the command


When there's only one possible form of arguments:






OpenGL Functions

When arguments can take multiple forms:

e.g. the Color function has the forms:






OpenGL Constants

Constants are defined for certain function arguments

Constant names are all in capital letters, in the form:

e.g. possible arguments for glBegin() are:






OpenGL Images

Images are drawn with glDrawPixels

glDrawPixels(width, height, format, type, pixels)

width & height are the size of the image, in pixels.

format is GL_RGB for RGB images, or GL_LUMINANCE for greyscale (there are other formats as well).

type indicates the data type of the pixel data. GL_UNSIGNED_BYTE is standard for 8-bit-per-channel images.

pixels is the actual array of image data. Use the tostring() function with PIL images.

    img = Image.open("foo.jpg")
    glDrawPixels(img.size[0], img.size[1], GL_RGB,
                 GL_UNSIGNED_BYTE, img.tostring())





OpenGL Images

The position to draw an image at is given with glRasterPos

glRasterPos2f(x, y)
or
glRasterPos3f(x, y, z)

Example: drawimage.py






DUE: Thursday, 22 September 2005, 11:59:59 pm
  (via e-mail, to depape@buffalo.edu)


Create a program that dynamically displays multiple images
the program must:

Some possibilities:

Extra credit will be given for making the program interactive - i.e. allow the user to control some part of what happens with the keyboard and/or mouse


E-mail your program, and any required images, to me at depape@buffalo.edu
Be sure to include your name, and the OS you wrote the program on, in comments in the code.



Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.