Raster Graphics and Python

pix
Go back to home page

This page provides an introduction to raster graphics and more experience with the Python programming language.

This page is obsolete.

The latest page is at itmetr.net.

last modified: 05:02 PM MDT, Sun 15 Jun 2008


Here you will learn some of the basics of:
  • raster graphics
  • color "science"
  • programming in Python
  • standard Linux tools for displaying and converting raster graphics: eog, convert, and display.

There is a lot of information here, probably too much for those truly unfamiliar with programming.

Raster graphics describe an image as an array of colored pixels. A 512x512 raster image displays with a size of about 7 inches by 7 inches on a typical monitor. When printed on a 600 dpi (dots per inch) printer at 7 inches by 7 inches, it will look "jagged", or blocky, just like the image in a monitor.

Vector graphics describe an image independent of the resolution of the final rendering device. The following sentence is a vector graphic: "Draw a circle 2 inches in diameter, filled with red, on a white background". When this is plotted on a 600 dpi printer it will look much smoother than the same graphic rendered on a monitor.

Vector graphics are obviously good for plots that can be described by objects (lines, circles, letters, etc.). Raster graphics are the only reasonable choice for photos and satellite images. Vector graphics can be converted to a raster graphic with a specified resolution, as is almost always the case before printing, except for "pen plotters". Raster graphics cannot be converted back to vector graphics. Certain compressed image formats, PNG for example, identifies pixels of the same color and describe a region to fill with the same color, rather than specifying a color for each pixel. But the PNG compression still specifies the raster image exactly. Circles are still jagged no matter what the resolution of the printer.


Making raster graphics

Here we will make portable pixel map files as our prime example of raster graphics. Using a text editor (gedit), put the following lines into a file a.ppm:

P3
3 2
255
255 0 0
0 255 0
0 0 255
255 255 0
200 200 0 
150 150 0

Now let's look at your image, with one or more of your available options. Your image is really tiny, three pixels by two pixels, and one pixel normally displays at about 1/72 of an inch on a monitor. So, we need to drastically enlarge the display of the image. Here are a few options on Gentry:

  • The following will display a.ppm 64 times the original (pixel) size:
    display -sample 6400% a.ppm
    

  • eog a.ppm
    
    Then go to Edit>>Preferences and turn OFF image interpolation. Then start clicking the "In" zoom icon.


Making PPM files with Python

Needless to say, you won't be completing your dissertation any time soon if you are going to manually type in RGB triplets for your graphics. Here we will use the Python programming language to write the RGB triplets to a file. Or, from the command line on Gentry:
wget http://gentry.metr.ou.edu/rgraphics/makeppm.py
Try this:
python makeppm.py
Alternatively, make makeppm.py executable with chmod u+x makeppm.py. Then run the program by typing the program name, e. g.:
makeppm.py

If that does not work, perhaps your current directory is not "in your path" (more on that later). Try:

./makeppm.py

If the last line in makeppm.py did not show you your colors.ppm, try it from the Linux command line:

display colors.ppm
You should see something like:

Use head colors.ppm or tail colors.ppm to see some of the text that makeppm.py made. Have a look inside makeppm.py using gedit and try to understand the syntax. Feel free to play with it; blend red and blue, for example. One thing you should realize about the python language is that the indentations define the scope of loops and nests, and the indentations must be consistent. I use one tab character, others prefer four spaces.


Compressing raster graphics

Use ls -l colors.ppm to see the size. When written in ascii, the file is quite large. You may want to try editting makeppm.py to use the "P6" binary format (see the comments in the code). Now look at the file size. Then

convert colors.ppm colors.png
convert colors.ppm colors.jpg

Use display colors.png of display colors.jpg to view what you made. Either one should look almost exactly like colors.ppm. Use ls -l colors.* to see the size of the files:

-rw-r--r--   1 yourname tux          2705 Dec 31 15:14 colors.jpg
-rw-r--r--   1 yourname tux           907 Dec 31 15:14 colors.png
-rw-r--r--   1 yourname tux        599055 Dec 31 11:34 colors.ppm

.png is the winner, which surprises me, because usually for smoothly varying colors the .jpg will be smaller. Not only that, but .jpg is a lossy compression, as this exercise demonstrates:

convert colors.ppm c.ppm
convert c.ppm colors.jpg
convert colors.jpg d.ppm
diff c.ppm d.ppm

Try the above exercise with .png, instead of .jpg, and you will see the .png is a reversible compression.

But if you download a typical .jpg: liberty.jpg, then:

convert liberty.jpg liberty.png

you will find the .png is larger.

convert is indeed a very useful command-line utility. Here is a short tutorial for convert.

Here is the official online documentation for convert, listing all of the command line options.


Task for Fall 2006

Download plotrean.py. Or from the Gentry command line:
wget http://gentry.metr.ou.edu/rgraphics/plotrean.py
chmod it to be executable. Then also on Gentry:
wgrib /REANALYSIS/1999/data/fixed/land.sfc -d 1 -text -o land.dat 
wgrib /REANALYSIS/1999/data/at00z12z/pres.sfc -d 1 -text -o pres1.dat 
plotrean.py land.dat
plotrean.py pres1.dat
You should see images like:
and .
Note the surface pressure field is dominated by the effect of topography. Your task is to plot pressure only over the water. Read the notes at the end of plotrean.py for details. I suggest you:
cp plotrean.py plotmask.py
or something like that, and hack away on plotmask.py to complete the task. Post your image, in PNG format, and your source code in your password protected site. Give the image an informative caption. Notify the instructor when it is ready for assessment.


Previous tasks now offered as examples

  • Plotting Julia Sets dabbles with programming in C, as well as Perl and Python. Worth a visit. Designing fractal images is fun.
  • Plotting Radar Data is a bit more practical than plotting fractals, for most us. This exercise required reading in numerical data from a text (ASCII) file, converting the polar coordinates of the data to the Cartesian coordinates of the plot. Uses C.

  • Plotting SST was the task for Spring 2003. It was rather simple, as compared with the above radar data exercise.

  • Plotting Bathymetry was the task for Spring 2004.

  • Plotting Sea Ice was the task for Spring 2005.


this is an obsolete site
go to new site
go to obsolete home page

pix
Move to top of page