Displaying Images

The graphics module also provides minimal support for displaying and manipulating images in a GraphWin. Most platforms will support at least PPM and GIF images. Display is done with an Image object. Images support the generic methods move(dx,dy), draw(graphwin), undraw(), and clone(). Image-specific methods are given below.

Image(anchorPoint, filename)
Constructs an image from contents of the given file, centered at the given anchor point. Can also be called with width and height parameters instead of filename. In this case, a blank (transparent) image is created of the given width and height (in pixels).


Example: flowerImage = Image(Point(100,100), "flower.gif")
Example: blankImage = Image(320, 240)

getAnchor()
Returns a clone of the point where the image is centered.


Example: centerPoint = flowerImage.getAnchor()

getWidth()
Returns the width of the image.


Example: widthInPixels = flowerImage.getWidth()

getHeight()
Returns the height of the image.


Example: heightInPixels = flowerImage.getHeight()

getPixel(x, y)
Returns a list [red, green, blue] of the RGB values of the pixel at position (x,y). Each value is a number in the range 0–255 indicating the intensity of the corresponding RGB color. These numbers can be turned into a color string using the color_rgb function (see next section).

Note that pixel position is relative to the image itself, not the window where the image may be drawn. The upper-left corner of the image is always pixel (0,0).


Example: red, green, blue = flowerImage.getPixel(32,18)

setPixel(x, y, color)
Sets the pixel at position (x,y) to the given color. Note: This is a slow operation.


Example: flowerImage.setPixel(32, 18, "blue")

save(filename)
Saves the image to a file. The type of the resulting file (e.g., GIF or PPM) is determined by the extension on the filename.


Example: flowerImage.save("mypic.ppm")