3. Image strings
3.1. Show image
- display.show(image)
- Display an image.
3.2. Image strings
- Image(string)
- Create an image.string is a string of 25 brightness values with every 5 separated by a colon.e.g. ‘01234:56789:09090:98765:43210’
Image('01234:56789:09090:98765:43210')
from microbit import *
display.show(Image('99999:90009:90009:90009:99999'))
from microbit import *
display.show(Image('11111:33333:55555:77777:99999'))
from microbit import *
display.show(Image('12345:23456:34567:45678:56789'))
Tasks
Write code for a large square of brightness 2.
Write code for a vertical brightness gradient of bright to dull from the top down to the bottom.
Write code for a horizontal brightness gradient of dull to bright from the left to right.
Write code for a diagonal brightness gradient of dull to bright from the bottom left to the top right.
Write code for a large square of brightness 2.
from microbit import *
display.show(Image('22222:20002:20002:20002:22222'))
Write code for a vertical brightness gradient of bright to dull from the top down to the bottom.
from microbit import *
display.show(Image('99999:77777:55555:33333:11111'))
Write code for a horizontal brightness gradient of dull to bright from the left to right.
from microbit import *
display.show(Image('13579:13579:13579:13579:13579'))
Write code for a diagonal brightness gradient of dull to bright from the bottom left to the top right.
from microbit import *
display.show(Image('56789:45678:34567:23456:12345'))
3.3. Image strings: Multiplication of a line
- string * integer
- Returns string + string + string …. integer times.
- Image(line_string * 5)
- line_string is the first 5 pixel brightness values. e.g. “00000:”e.g. Image(“00000:” * 5) create an image with all pixels off.e.g. Image(“99999:” * 5) create an image with all pixels at full brightness.
from microbit import *
img = Image("13579:" * 5)
display.show(img)
Tasks
Modify the code above to go from dim on the left edge to bright in the middle to dim on the right edge.
Modify the code above to go from bright on the left edge to dim on the right edge.
Write code to cycle between bright on the left edge to bight on the right edge.
Modify the code above to go from dim on the left edge to bright in the middle to dim on the right edge.
from microbit import *
img = Image("15951:" * 5)
display.show(img)
Modify the code above to go from bright on the left edge to dim on the right edge.
from microbit import *
img = Image("97531:" * 5)
display.show(img)
Write code to cycle between bright on the left edge to bight on the right edge.
from microbit import *
while True:
img1 = Image("97531:" * 5)
display.show(img1)
sleep(300)
img2 = Image("13579:" * 5)
display.show(img2)
sleep(300)
3.4. Image strings: line by line
from microbit import *
large_square = Image('99999:'
'90009:'
'90009:'
'90009:'
'99999')
display.show(large_square)
Tasks
Write code for a large square of brightness 3 by lining up the 5 rows of the image under each other.
Write code for a small central square of brightness 9 by lining up the 5 rows of the image under each other.
Write code for 2 symmetrically spaced central horizontal lines of brightness 5 by lining up the 5 rows of the image under each other.
Write code for 2 symmetrically spaced central vertical lines of brightness 5 by lining up the 5 rows of the image under each other.
Write code for a large square of brightness 3 by lining up the 5 rows of the image under each other.
from microbit import *
large_square = Image('33333:'
'30003:'
'30003:'
'30003:'
'33333')
display.show(large_square)
Write code for a small central square of brightness 9 by lining up the 5 rows of the image under each other.
from microbit import *
small_square = Image('00000:'
'09990:'
'09090:'
'09990:'
'00000')
display.show(small_square)
Write code for 2 symmetrically spaced central horizontal lines of brightness 5 by lining up the 5 rows of the image under each other.
from microbit import *
hor_lines = Image('00000:'
'55555:'
'00000:'
'55555:'
'00000')
display.show(hor_lines)
Write code for 2 symmetrically spaced central vertical lines of brightness 5 by lining up the 5 rows of the image under each other.
from microbit import *
vert_lines = Image('05050:'
'05050:'
'05050:'
'05050:'
'05050')
display.show(vert_lines)
3.5. Boat sinking animation
display.show
can use a list of images, the list of custom images can be shown in sequence, making an animation.from microbit import *
boat1 = Image('05050:'
'05050:'
'05050:'
'99999:'
'09990')
boat2 = Image('00000:'
'05050:'
'05050:'
'05050:'
'99999')
boat3 = Image('00000:'
'00000:'
'05050:'
'05050:'
'05050')
boat4 = Image('00000:'
'00000:'
'00000:'
'05050:'
'05050')
boat5 = Image('00000:'
'00000:'
'00000:'
'00000:'
'05050')
boat6 = Image('00000:'
'00000:'
'00000:'
'00000:'
'00000')
sinking_boats = [boat1, boat2, boat3, boat4, boat5, boat6]
while True:
display.show(sinking_boats, delay=500)
Tasks
Write a list variable,
rising_boats
, that lists the boats in reverse order and animates a rising boat. Rather than manually listing the order, uselist(reversed(sinking_boats))
.Combine the 2 animations to show a boat sinking and rising over and over again.
Write a list variable, rising_boats
, that lists the boats in reverse order and animates a rising boat. Rather than manually listing the order, use list(reversed(sinking_boats))
.
from microbit import *
boat1 = Image("05050:" "05050:" "05050:" "99999:" "09990")
boat2 = Image("00000:" "05050:" "05050:" "05050:" "99999")
boat3 = Image("00000:" "00000:" "05050:" "05050:" "05050")
boat4 = Image("00000:" "00000:" "00000:" "05050:" "05050")
boat5 = Image("00000:" "00000:" "00000:" "00000:" "05050")
boat6 = Image("00000:" "00000:" "00000:" "00000:" "00000")
sinking_boats = [boat1, boat2, boat3, boat4, boat5, boat6]
rising_boats = list(reversed(sinking_boats))
while True:
display.show(rising_boats, delay=500)
Combine the 2 animations to show a boat sinking and rising over and over again.
from microbit import *
boat1 = Image("05050:" "05050:" "05050:" "99999:" "09990")
boat2 = Image("00000:" "05050:" "05050:" "05050:" "99999")
boat3 = Image("00000:" "00000:" "05050:" "05050:" "05050")
boat4 = Image("00000:" "00000:" "00000:" "05050:" "05050")
boat5 = Image("00000:" "00000:" "00000:" "00000:" "05050")
boat6 = Image("00000:" "00000:" "00000:" "00000:" "00000")
sinking_boats = [boat1, boat2, boat3, boat4, boat5, boat6]
rising_boats = list(reversed(sinking_boats))
while True:
display.show(sinking_boats, delay=500)
display.show(rising_boats, delay=500)
3.6. Rotating line animation
3.7. Image gradients using string definitions
from microbit import *
def horz_grad(w, h):
string = ""
for i in range(1, w + 1):
string += str(i)
return (string + ":") * h
img_horz_grad = Image(horz_grad(5, 5))
display.show(img_horz_grad)
Tasks
Write a vert_grad definition to produce: Image(‘11111:22222:33333:44444:55555:’).
Write a diag_grad definition to produce: Image(‘12345:23456:34567:45678:56789:’).
Write a vert_grad definition to produce: Image(‘11111:22222:33333:44444:55555:’).
from microbit import *
def vert_grad(w, h):
string = ""
for i in range(1, h + 1):
string += (str(i) * w) + ":"
return string
img_vert_grad = Image(vert_grad(5, 5))
display.show(img_vert_grad)
Write a diag_grad definition to produce: Image(‘12345:23456:34567:45678:56789:’).
from microbit import *
def diag_grad(w, h):
string = ""
for j in range(1, h + 1):
for i in range(1, w + 1):
string += str(i + j - 1)
string += ":"
return string
img_diag_grad = Image(diag_grad(5, 5))
display.show(img_diag_grad)