9. Byte array images
9.1. bytearray with integer list
- Image(width=None, height=None, buffer=None)
- Creates an image with width columns and height rows.The buffer can be a list of integers passed to bytearray as in bytearray(integer_list).
from microbit import *
new_img = Image(5, 5, bytearray([1,1,1,1,1,3,3,3,3,3,5,5,5,5,5,7,7,7,7,7,9,9,9,9,9]))
display.show(new_img)
from microbit import *
my_image_diag_grad = Image(5, 5, bytearray([
1, 2, 3, 4, 5,
2, 3, 4, 5, 6,
3, 4, 5, 6, 7,
4, 5, 6, 7, 8,
5, 6, 7, 8, 9,
]))
my_image_horz_grad = Image(5, 5, bytearray([
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
]))
my_image_vert_grad = Image(5, 5, bytearray([
1, 1, 1, 1, 1,
2, 2, 2, 2, 2,
3, 3, 3, 3, 3,
4, 4, 4, 4, 4,
5, 5, 5, 5, 5,
]))
while True:
display.show(my_image_horz_grad)
sleep(500)
display.show(my_image_vert_grad)
sleep(600)
display.show(my_image_diag_grad)
sleep(700)
display.clear()
sleep(700)
9.2. Uniform images using List multiplication
from microbit import *
img = Image(5, 5, bytearray([1] * 25))
display.show(img)
Tasks
Modify the code to produce an image of uniform brightness of 3.
Modify the code to produce an image of uniform brightness of 7.
Modify the code to produce an image of uniform brightness of 3.
from microbit import *
img = Image(5, 5, bytearray([3] * 25))
display.show(img)
Modify the code to produce an image of uniform brightness of 7.
from microbit import *
img = Image(5, 5, bytearray([7] * 25))
display.show(img)
9.3. bytearray gradients using List multiplication
from microbit import *
img = Image(5, 5, bytearray([1, 3, 5, 7, 9] * 5))
display.show(img)
Tasks
Modify the code to produce a horizontal gradient of decreasing brightness.
Modify the code to produce a horizontal gradient of decreasing then increasing brightness.
Modify the code to produce a horizontal gradient of decreasing brightness.
from microbit import *
img = Image(5, 5, bytearray([9, 7, 5, 3, 1] * 5))
display.show(img)
Modify the code to produce a horizontal gradient of decreasing then increasing brightness.
from microbit import *
img = Image(5, 5, bytearray([9, 6, 3, 6, 9] * 5))
display.show(img)
9.4. bytearray irregular patterns using List multiplication and concatenation
from microbit import *
img = Image(5, 5, bytearray([1, 9] * 12 + [1]))
display.show(img)
Tasks
Modify the code to produce an alternating pattern of brightness 9 and 4.
Modify the code to produce a cyclic pattern of brightness 9 , 1 and 1.
Modify the code to produce an alternating pattern of brightness 9 and 4.
from microbit import *
img = Image(5, 5, bytearray([9, 4] * 12 + [9]))
display.show(img)
Modify the code to produce a cyclic pattern of brightness 9 , 1 and 1.
from microbit import *
img = Image(5, 5, bytearray([9, 1, 1] * 8 + [9]))
display.show(img)
9.5. Integer-list-producing definitions for bytearray
value = x_val1 + (x * x_step)
# is improved to make sure integers between 0 and 9 are produced
value = min(9, max(0, int(x_val1 + (x * x_step))))
from microbit import *
def gradient_x(x_val1, x_step):
grid = []
for y in range(5):
for x in range(5):
new_val = min(9, max(0, int(x_val1 + (x * x_step))))
grid.append(new_val)
return grid
img_array = gradient_x(1, 2)
img_horz_grad = Image(5, 5, bytearray(img_array))
display.show(img_horz_grad)
Tasks
Use the definition for a horizontal gradient to create a gradient horizontally from 7 in the left to 3 in the right.
Modify the code to create a vertical gradient from 1 in the top to 9 in the bottom using a definition to produce the gradient list: [1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9]
Use the definition for a vertical gradient to create one from 9 in the top to 2 in the bottom. [9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2], and display it.
Write a definition to produce the list to be used for a diagonal gradient from 1 in the top left to 9 in the bottom right. [1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8, 5, 6, 7, 8, 9], and display it.
Use the definition for a horizontal gradient to create a gradient horizontally from 7 in the left to 3 in the right.
from microbit import *
def gradient_x(x_val1, x_step):
grid = []
for y in range(5):
for x in range(5):
new_x = min(9, max(0, int(x_val1 + (x * x_step))))
grid.append(new_x)
return grid
img_array = gradient_x(7, -1)
img_horz_grad = Image(5, 5, bytearray(img_array))
display.show(img_horz_grad)
Modify the code to create a vertical gradient from 1 in the top to 9 in the bottom using a definition to produce the gradient list: [1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9]
from microbit import *
def gradient_y(y_val1, y_step):
grid = []
for y in range(5):
new_y = min(9, max(0, int(y_val1 + (y * y_step))))
for x in range(5):
grid.append(new_y)
return grid
img_array = gradient_y(1, 2)
img_vert_grad = Image(5, 5, bytearray(img_array))
display.show(img_vert_grad)
Use the definition for a vertical gradient to create one from 9 in the top to 2 in the bottom, [9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2], and display it.
from microbit import *
def gradient_y(y_val1, y_step):
grid = []
for y in range(5):
new_y = min(9, max(0, int(y_val1 + (y * y_step))))
for x in range(5):
grid.append(new_y)
return grid
img_array = gradient_y(9, -8 / 5)
img_vert_grad = Image(5, 5, bytearray(img_array))
display.show(img_vert_grad)
Write a definition to produce the list to be used for a diagonal gradient from 1 in the top left to 9 in the bottom right, [1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8, 5, 6, 7, 8, 9], and display it.
from microbit import *
def gradient_xy(xy_val1, x_step, y_step):
grid = []
for y in range(5):
y_add = min(9, max(0, (y * y_step)))
for x in range(5):
x_add = min(9, max(0, (x * x_step)))
new_xy = min(9, max(0, int(xy_val1 + x_add + y_add)))
grid.append(new_xy)
return grid
img_array = gradient_xy(1, 1, 1)
img_vert_grad = Image(5, 5, bytearray(img_array))
display.show(img_vert_grad)
9.6. Random-integer-producing definitions for bytearray
from microbit import *
from random import randint
def full_screen_fill_bytes(min_brightness=1, max_brightness=9):
screen_bytes = []
for y in range(0, 5):
for x in range(0, 5):
brightness = randint(min_brightness, max_brightness)
screen_bytes.append(brightness)
return screen_bytes
while True:
screen_bytes = full_screen_fill_bytes(1, 9)
new_img = Image(5, 5, bytearray(screen_bytes))
display.show(new_img)
sleep(1000)
Tasks
Modify the code to add 2 nested for-loops for x values so that the left and right edges use the max_brightness value.
Modify the code to add 2 nested for-loops for x values so that the left and right edges use the max_brightness value.
from microbit import *
from random import randint
def full_screen_fill_bytes(min_brightness=1, max_brightness=9):
screen_bytes = []
for y in range(0, 5):
for x in range(0, 1):
brightness = max_brightness
screen_bytes.append(brightness)
for x in range(1, 4):
brightness = randint(min_brightness, max_brightness)
screen_bytes.append(brightness)
for x in range(4, 5):
brightness = max_brightness
screen_bytes.append(brightness)
return screen_bytes
while True:
screen_bytes = full_screen_fill_bytes(1, 9)
new_img = Image(5, 5, bytearray(screen_bytes))
display.show(new_img)
sleep(1000)
9.7. List comprehension for bytearray images
See: https://www.w3schools.com/python/python_lists_comprehension.asp
- new_list = [expression for item in iterable]
- Create a list of the results of expressions that take each item in an iterable, such as a list, tuple or string.
from microbit import *
brightness_array = [int(i/3 + 1) for i in range(25)]
new_img = Image(5, 5, bytearray(brightness_array))
display.show(new_img)
from microbit import *
import random
while True:
brightness_array = [random.randint(0, 9) for _ in range(25)]
new_img = Image(5, 5, bytearray(brightness_array))
display.show(new_img)
sleep(300)
Tasks
Modify the code to produce an image with pixels at a brightness determined by the formula: int(((i % 3) + 1) * 3). This creates a cyclic pattern of 3.
Modify the code to produce images made up of random brightnesses chosen from the list [1, 5, 9].
Modify the code to produce an image with pixels at a brightness determined by the formula: int(((i % 3) + 1) * 3). This creates a cyclic pattern of 3.
from microbit import *
brightness_array = [int(((i % 3) + 1) * 3) for i in range(25)]
new_img = Image(5, 5, bytearray(brightness_array))
display.show(new_img)
Modify the code to produce images made up of random brightnesses chosen from the list [1, 5, 9].
from microbit import *
import random
while True:
brightness_array = [random.choice([1, 5, 9]) for _ in range(25)]
new_img = Image(5, 5, bytearray(brightness_array))
display.show(new_img)
sleep(500)
9.8. List comprehension with alternatives for bytearray images
See: https://www.w3schools.com/python/python_lists_comprehension.asp
- new_list = [expression if condition else expression2 for item in iterable]
- Create a list of expressions that take each item in an iterable, such as a list, tuple or string.
from microbit import *
brightness_array = [9 if i % 2 == 0 else 0 for i in range(25)]
new_img = Image(5, 5, bytearray(brightness_array))
display.show(new_img)
Tasks
Modify the code set the brightness to 9 when i mod 1 is 1 instead of 0.
Modify the code to use mod 3 instead of mod 2.
Modify the code set the brightness to 9 when i mod 1 is 1 instead of 0.
from microbit import *
brightness_array = [9 if i % 2 == 1 else 0 for i in range(25)]
new_img = Image(5, 5, bytearray(brightness_array))
display.show(new_img)
Modify the code to use mod 3 instead of mod 2.
from microbit import *
brightness_array = [9 if i % 3 == 0 else 0 for i in range(25)]
new_img = Image(5, 5, bytearray(brightness_array))
display.show(new_img)