My LEGO EV3 robot loves Vancouver, here is what it says:
This project shows how to draw texts and PNG images on EV3 Intelligent brick screen. Please refer to my previous post LEGO EV3 MicroPython - Random Shapes on how to install MicroPython image for LEGO EV3.
The media Module
LEGO EV3 MicroPython
media module describes media such as fonts, images, and sounds.
Font class in
media.ev3dev represents a font for writing text. I want a bigger font for the texts on the screen so I set the font size to 64 pixels and bold.
To load an image is simple, the
Image class in
media.ev3dev takes a
source parameter. If
source is a stirng, the image will be loaded from the file path given by the string. Only PNG files are supported.
from pybricks.hubs import EV3Brick from pybricks.media.ev3dev import Font from pybricks.media.ev3dev import Image ev3 = EV3Brick() text_font = Font(size=64, bold=True) ev3.screen.set_font(text_font) heart_image = Image('heart.png')
There are a little bit calculations and trial and error steps to have the texts in the middle of the screen and the heart image fits the gap between “I” and “VAN”. The heart image is scaled down to 64x64 pixels.
Since the EV3 screen size is 178x128 pixels and my font size is set to 64 pixels, I place the “I” text at position (16, 48). The “VAN” text is at position (100, 48). This will leave enough space for the heart image in-between.
# draw and say 'I' ev3.screen.draw_text(16, 48, 'I') ev3.speaker.say('I') # draw 'VAN' and say 'Vancouver' ev3.screen.draw_text(100, 48, 'VAN') ev3.speaker.say('Vancouver')
To draw the image, I have to resize the heart PNG image first to fit the screen. EV3 Intelligent Brick has a monochrome LCD display so color images will be displayed in grayscale.
The PNG image I am using has a transparent background. From my testing, EV3 MicroPython will convert the transparent background to
# draw heart image and say 'love' # the transparent background is black ev3.screen.draw_image(32, 32, heart_image, transparent=Color.BLACK) ev3.speaker.say('love')
EV3Brick Class Reference
Sets the font used for writing on the screen.
screen.draw_image(x, y, source, transparent=None)
source image on the screen.
screen.draw_text(x, y, text, text_color=Color.BLACK, background_color=None)
Draws text on the screen.