Old-Timey Snapchat for the Microbit
As a I mentioned in my other post from today about the microbit radio API, teaching the maker class gave me some new ideas for playing with the microbit. One of the students asked me if it was possible, since we could send text to another device, could we also send a picture because Image objects are created using colon separated strings of intensity values. I tried to write a skeleton on a keynote slide to get them going but it didn't seem to work correctly. I had hoped that the str() function or the repr() function might have given me a serializable form of an image such that I could send it to another device. Unfortunately the text returned from either of these functions wasn't the right format to be imported directly by another microbit. I had to create my own function to iterate across the image, reading each pixel and building up a string in the correct format.
# Old-timey snapchat
from microbit import *
import radio
def convert_image_to_text(image):
text = ""
# append each pixel in 5 x 5 grid to line of text.
for x in range(5):
for y in range(5):
text = text + str(image.get_pixel(x, y))
# add in colon delimiters so microbit can
# work out it's an image
if x < 4:
text = text + ":"
return text
radio.on()
pic_to_send = Image("90909:09090:90909:09090:90909")
while True:
if button_a.was_pressed():
pic_as_text = convert_image_to_text(pic_to_send)
radio.send(pic_as_text)
received_pic_as_text = radio.receive()
if received_pic_as_text:
received_pic = Image(received_pic_as_text)
display.show(received_pic)
sleep(5000)
display.clear()