Image

class Image(source, sub=False)

Object representing a graphics image. This can either be an in-memory copy of an image or the image displayed on a screen.

Parameters:
  • source (Image) – The source image. The new object will contain a copy of the source image object.

  • sub (bool) –

    If sub is True, then the image object will act as a sub-image of the source image.

    Additional keyword arguments x1, y1, x2, y2 are needed when sub=True. These specify the top-left and bottom-right coordinates in the source image that will be used as the bounds for the sub-image.

static empty(width=178, height=128) Image

Creates a new empty Image object.

Parameters:
  • width (int) – The width of the image in pixels.

  • height (int) – The height of the image in pixels.

Returns:

A new image with all pixels set to Color.WHITE.

Raises:
  • TypeError – If width or height is not a number.

  • ValueError – If width or height is less than 1.

  • RuntimeError – If there was a problem allocating a new image.

Drawing text

There are two ways to draw text on images. draw_text() lets text be placed precisely on the image or print() can be used to automatically print text on a new line.

draw_text(x, y, text, text_color=Color.BLACK, background_color=None)

Draws text on this image.

The most recent font set using set_font() will be used or Font.DEFAULT if no font has been set yet.

Parameters:
  • x (int) – The x-axis value where the left side of the text will start.

  • y (int) – The y-axis value where the top of the text will start.

  • text (str) – The text to draw.

  • text_color (Color) – The color used for drawing the text.

  • background_color (Color) – The color used to fill the rectangle behind the text or None for transparent background.

print(*args, sep=' ', end='\n')

Prints a line of text on this image.

This method works like the builtin print() function, but it writes on this image instead.

You can set the font using set_font(). If no font has been set, Font.DEFAULT will be used. The text is always printed used black text with a white background.

Unlike the builtin print(), the text does not wrap if it is too wide to fit on this image. It just gets cut off. But if the text would go off of the bottom of this image, the entire image is scrolled up and the text is printed in the new blank area at the bottom of this image.

Parameters:
  • args (Any) – Zero or more objects to print.

  • sep (str) – Separator that will be placed between each object that is printed.

  • end (str) – End of line that will be printed after the last object.

set_font(font)

Sets the font used for writing on this image.

The font is used for both draw_text() and print().

Parameters:

font (Font) – The font to use.

Drawing images

A copy of another image can be drawn on an image. Also consider using sub-images to copy part of an image.

draw_image(x, y, source, transparent=None)

Draws the source image on this image.

Parameters:
  • x (int) – The x-axis value where the left side of the image will start.

  • y (int) – The y-axis value where the top of the image will start.

  • source (Image) – The source Image.

  • transparent (Color) – The color of image to treat as transparent or None for no transparency.

Drawing shapes

These are the methods to draw basic shapes, including points, lines, rectangles and circles.

draw_pixel(x, y, color=Color.BLACK)

Draws a single pixel on this image.

Parameters:
  • x (int) – The x coordinate of the pixel.

  • y (int) – The y coordinate of the pixel.

  • color (Color) – The color of the pixel.

draw_line(x1, y1, x2, y2, width=1, color=Color.BLACK)

Draws a line on this image.

Parameters:
  • x1 (int) – The x coordinate of the starting point of the line.

  • y1 (int) – The y coordinate of the starting point of the line.

  • x2 (int) – The x coordinate of the ending point of the line.

  • y2 (int) – The y coordinate of the ending point of the line.

  • width (int) – The width of the line in pixels.

  • color (Color) – The color of the line.

draw_box(x1, y1, x2, y2, r=0, fill=False, color=Color.BLACK)

Draws a box on this image.

Parameters:
  • x1 (int) – The x coordinate of the left side of the box.

  • y1 (int) – The y coordinate of the top of the box.

  • x2 (int) – The x coordinate of the right side of the box.

  • y2 (int) – The y coordinate of the bottom of the box.

  • r (int) – The radius of the corners of the box.

  • fill (bool) – If True, the box will be filled with color, otherwise only the outline of the box will be drawn.

  • color (Color) – The color of the box.

draw_circle(x, y, r, fill=False, color=Color.BLACK)

Draws a circle on this image.

Parameters:
  • x (int) – The x coordinate of the center of the circle.

  • y (int) – The y coordinate of the center of the circle.

  • r (int) – The radius of the circle.

  • fill (bool) – If True, the circle will be filled with color, otherwise only the circumference will be drawn.

  • color (Color) – The color of the circle.

Image properties

width

Gets the width of this image in pixels.

height

Gets the height of this image in pixels.

Replacing the entire image

clear()

Clears this image. All pixels on this image will be set to Color.WHITE.

load_image(source)

Clears this image, then draws the source image centered in this image.

Parameters:

source (Image) – The source Image.