| ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ |
|---|
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
sourceimage object.sub (bool) –
If
subisTrue, then the image object will act as a sub-image of thesourceimage.Additional keyword arguments
x1,y1,x2,y2are needed whensub=True. These specify the top-left and bottom-right coordinates in thesourceimage that will be used as the bounds for the sub-image.
- static empty(width=178, height=128) Image
Creates a new empty
Imageobject.- Parameters:
- Returns:
A new image with all pixels set to
Color.WHITE.- Raises:
TypeError – If
widthorheightis not a number.ValueError – If
widthorheightis 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 orprint()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 orFont.DEFAULTif 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
Nonefor 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.DEFAULTwill 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.
- set_font(font)
Sets the font used for writing on this image.
The font is used for both
draw_text()andprint().- 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
sourceimage on this image.
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.
- 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 withcolor, 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:
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.