Raster Graphics

Introduction

Raster images (see Raster Graphics) contain an array of pixels, each with an individual color assignment composed of shades of red, green, and blue (RGB) for display on a computer monitor. A value between 0 and 255 (or 00 and FF in hexadecimal notation) determines the intensity of each of these three colors for a particular pixel. Visit this explanation of hexadecimal numbers and how we use them to describe colors.

The appearance of a raster image is not independent of the size of the image. Zooming in on a raster image degrades the apparent quality of the image. Alternatively, vector graphics use points, lines, curves, and polygons to construct an image that can be scaled without degrading quality. Try zooming in as much as possible on the text in a PDF document drawn with vector graphics to understand this important difference.

There are several formats for raster graphics. You may have seen many of the more common formats (e.g., JPEG, TIFF, GIF, PNG, Windows bitmap). Here, you will work with portable pixmap file format (PPM) files, since these are very easy to write with a simple program. PPM is one of three formats collectively know as Netpbm formats. After reading about Netpbm formats and studying the examples, you can learn more about using PPM to make high-quality color pictures.

Writing PPM Files with Python

Grab first.py and make it executable with chmod u+x first.py Then run the program and view the result with ./first.py
eog first.ppm &
Depending on the green color that you chose, your image should resemble the image below.

first
Figure 1: An image produced by first.py with green fixed at 125.

Look at some of the text in first.ppm by using head first.ppm
tail first.ppm
Then look at first.py and try to understand the syntax and what the program does.

Image Compression

Your small image is actually quite large when written with ASCII characters. View the size of the file with ls -l first.ppm You can also use ls -lh first.ppm to see the size in bytes and kilobytes. Try changing the file type to binary (P6) in first.py to see how the size changes. Now use ImageMagick to convert the PPM file to both a Portable Network Graphics (PNG) image and a Joint Photographic Experts Group (JPEG) image: convert first.ppm first.png
convert first.ppm first.jpg
Look at these images and note that they are exactly the same as first.ppm. Again using ls -l first.* you can see that the PNG image is smaller, which may not always occur. PNG images are special in that the format employs lossless data compression. On the other hand, JPEG uses a lossy compression algorithm. To see this, we can convert back and forth between JPEG and PNG images: convert first.ppm A.ppm
convert A.ppm temp.jpg
convert temp.jpg B.ppm
diff A.ppm B.ppm
Repeat the above steps using .png instead of .jpg to see that PNG is a reversible compression. The files are exactly the same, shown by the fact that the diff command does not return anything.

Raster image sizes can impact your selection of the appropriate format. Download redadmiral.jpg and then convert redadmiral.jpg redadmiral.png

butterflybutterfly
Figure 2: Red admiral butterfly photographed in Gainesville, TX on 6 May 2001 shown in both JPEG (left) and PNG (right) formats. Photo © C. Godfrey.

The displayed images (above) appear identical, but the PNG image is 97 kilobytes and the JPEG image is 38 kilobytes.

Fractional Vegetation Coverage

As a meteorologist, you may work with satellite observations at some point. You may want to visualize the data before manipulating them in a computer program or numerical weather prediction model. On Blizzard, grab fveg.tar using wget and then unpack the file with tar xvf fveg.tar in the directory where you plan to work. You now have several files:

  • vegimg.py is a Python program that you will use to complete your assignment.
  • fvegMOD15A2.txt is a text file containing the percentage (0-100%) of photosynthetically active vegetation in 1-km grid cells derived from the maximum NDVI composite for the period 29 September—6 October 2008.
  • format.txt describes the format of the data file.

The raw observations were obtained by the MODIS sensor onboard the Terra polar orbiting satellite. The Land Processes Distributed Active Archive Center provides the processed data at no charge. I have manipulated the data a bit to produce the text file provided here.

The Python program should work straight out of the box: ./vegimg.py fvegMOD15A2.txt The program produces an image and a color bar in PPM format, both of which you can view using eog. Open a PPM file in a text editor to see the header before the binary RGB color information.

snow Your job, detailed in the Python code, requires assigning appropriate colors to the vegetation fraction information (the default colors are probably not the best choice). Then assign different colors to special values such as water, urban areas, and other types of non-vegetative land cover. After you have produced a satisfactory color scheme, use Inkscape to combine your image with the color bar and add notation. Then convert your image to a PNG file and post it, along with your modified Python code, on your password-protected Web page. (See below for tips on using Inkscape and converting file formats.) Your finished image might look something like mine below.

fveg
Figure 3: MODIS/Terra 1-km fraction of photosynthetically active vegetation (8-day composite) for the period 29 September—6 October 2008 showing North Carolina and South Carolina. Click for a larger image.

Inkscape and Image Formatting Tips

The free graphics editor Inkscape excels at creating and manipulating vector graphics, but it can also handle raster images. Start Inkscape and open your image file with inkscape vegimg.ppm & The & allows you to continue working on Blizzard. Ignore any error messages that may appear by hitting Enter in your SSH terminal. Add your color bar with File > Import... and browse to the location of the file. After importing, you may need to adjust the size of the color bar. Saving the file in Scalable Vector Graphics (SVG) format allows you to return later and manipulate your color bar and annotation. To save your work, try File > Save As... and enter the name of your file. Then select the folder in which to save the file by browsing through the folders. Do not underestimate the importance of the latter step. If you do not select the appropriate folder, Inkscape will crash.

If you find that your Internet connection speed is just too slow to make working with Inkscape via Blizzard a viable option, then you may download Inkscape for free and use it locally.

Exporting the image file worked best for me using File > Export Bitmap... In the dialog box, you can select the bitmap size and then save the file as a PNG image. You can further manipulate the image using ImageMagick. At the command prompt, use convert -help|more to learn about the options for using this powerful image editor. You could also see the command line options for convert or visit this ImageMagick tutorial.

North Carolina's Public Liberal Arts University