11. Flip and Rotate images
11.1. Useful Syntax
11.1.1. Image repr
- repr(image)
- Get a compact string representation of the image.e.g repr(Image.DUCK) returns Image(‘09900:99900:09999:09990:00000:’)
11.1.2. String replace
- string.replace(oldvalue, newvalue, count)
- oldvalue - The string to search fornewvalue - The string to replace the old value withcount (optional) - A number specifying how many occurrences of the old value to be replaced. Defaults to all occurrences if omitted.e.g. img_str = img_str.replace(“:”, “”)
11.1.3. Reverse list method syntax
- a_list.reverse()
- Reverses a list. No parameters are involved.e.g. [9, 9, 9, 0, 0] beis reversed to become [0, 0, 9, 9, 9]
11.2. Pixels from repr
from microbit import *
img = Image.DUCK
img_repr = repr(img)
print(img_repr)
# Image('09900:99900:09999:09990:00000:')
11.3. Brightness array
from microbit import *
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
from microbit import *
img_array = []
for x in img_str:
img_array.append(int(x))
Tasks
Put together the steps above to write a function, get_image_array(img), that returns an image array from an image. So the code converts Image.DUCK to Image(‘09900:99900:09999:09990:00000:’) then returns [0, 9, 9, 0, 0, 9, 9, 9, 0, 0, 0, 9, 9, 9, 9, 0, 9, 9, 9, 0, 0, 0, 0, 0, 0].
Put together the steps above to write a function, get_image_array(img).
from microbit import *
def get_image_array(img):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
11.4. Next steps
flipping horizontally
flipping vertically
rotating 90 degrees clockwise or 90 anticlockwise.
11.5. Flipping horizontally
Tasks
Complete the code to flip the duck horizontally and swap between the display of the duck and the flipped duck.
from microbit import * def get_image_array(img): img_repr = repr(img) img_str = img_repr[7:-3] img_str = img_str.replace(":", "") img_array = [int(x) for x in img_str] return img_array def get_imgarr_flip_hor(imgarray): # get every 5 elements and reverse them row0 = imgarray[0:5] row1 = row2 = row3 = row4 = row0.reverse() row1. row2. row3. row4. output_array = row0 + return output_array def get_image_flip_hor(img): img_array = img_flip_hor= Image(5, 5, bytearray(img_array)) return img = Image.DUCK img_flip_hor = while True: display.show(img) sleep(300) display.show(img_flip_hor) sleep(300)
Write code to flip the duck horizontally and swap between the display of the duck and the flipped duck.
from microbit import *
def get_image_array(img):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_flip_hor(imgarray):
# get every 5 elements and reverse them
row0 = imgarray[0:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:25]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_image_flip_hor(img):
img_array = get_imgarr_flip_hor(get_image_array(img))
img_flip_hor= Image(5, 5, bytearray(img_array))
return img_flip_hor
img = Image.DUCK
img_flip_hor = get_image_flip_hor(img)
while True:
display.show(img)
sleep(300)
display.show(img_flip_hor)
sleep(300)
imgarray[i:i+5]
.for i in range(0, len(imgarray), 5)
Tasks
Modify get_imgarr_flip_hor to use a for-loop.
from microbit import * def get_imgarr_flip_hor(imgarray): # get every 5 elements and reverse them row0 = imgarray[0:5] row1 = imgarray[5:10] row2 = imgarray[10:15] row3 = imgarray[15:20] row4 = imgarray[20:25] row0.reverse() row1.reverse() row2.reverse() row3.reverse() row4.reverse() output_array = row0 + row1 + row2 + row3 + row4 return output_array
Modify get_imgarr_flip_hor to use a for-loop.
from microbit import *
def get_imgarr_flip_hor(imgarray):
output_array = []
for i in range(0, len(imgarray), 5):
row = imgarray[i:i+5]
row.reverse()
output_array += row
return output_array
from microbit import *
def get_image_array(img):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_flip_hor(imgarray):
output_array = []
for i in range(0, len(imgarray), 5):
row = imgarray[i:i+5]
row.reverse()
output_array += row
return output_array
def get_image_flip_hor(img):
img_array = get_imgarr_flip_hor(get_image_array(img))
img_flip_hor= Image(5, 5, bytearray(img_array))
return img_flip_hor
img = Image.DUCK
img_flip_hor = get_image_flip_hor(img)
while True:
display.show(img)
sleep(300)
display.show(img_flip_hor)
sleep(300)
11.6. List comprehension for get_imgarr_flip_hor
from microbit import *
def reverse_list(input_list):
input_list.reverse()
return input_list
def get_imgarr_flip_hor(imgarray):
return [pixel for i in range(0, len(imgarray), 5) for pixel in reverse_list(imgarray[i:i+5])]
11.7. Flipping vertically
Tasks
Modify get_imgarr_flip_vert to use a for-loop.
from microbit import * def get_imgarr_flip_vert(imgarray): # get every 5 elements as rows and reverse order of rows. row0 = imgarray[0:5] row1 = imgarray[5:10] row2 = imgarray[10:15] row3 = imgarray[15:20] row4 = imgarray[20:25] output_array = row4 + row3 + row2 + row1 + row0 return output_array
Modify get_imgarr_flip_vert to use a for-loop.
from microbit import *
def get_imgarr_flip_vert(imgarray):
output_array = []
for i in range(4, -1, -1):
row = imgarray[i*5:(i+1)*5]
output_array += row
return output_array
Tasks
Write code to flip the duck vertically and swap between the display of the duck and the flipped duck.
Write code to flip the duck vertically and swap between the display of the duck and the flipped duck.
from microbit import *
def get_image_array(img):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_flip_vert(imgarray):
output_array = []
for i in range(4, -1, -1):
row = imgarray[i*5:(i+1)*5]
output_array += row
return output_array
def get_image_flip_vert(img):
img_array = get_imgarr_flip_vert(get_image_array(img))
img_flip_vert= Image(5, 5, bytearray(img_array))
return img_flip_vert
img = Image.DUCK
img_flip_vert = get_image_flip_vert(img)
while True:
display.show(img)
sleep(300)
display.show(img_flip_vert)
sleep(300)
11.8. Flipping a list of images
Tasks
Write code to rotate a list of images using flip_imagelist(image_list, transition_time=300).
Write code to rotate a list of images using rotate_imagelist(image_list, transition_time=500).
from microbit import *
def get_image_array(img=Image.DUCK):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_flip_hor(imgarray):
output_array = []
for i in range(0, len(imgarray), 5):
row = imgarray[i:i+5]
row.reverse()
output_array += row
return output_array
def get_image_flip_hor(img):
img_array = get_imgarr_flip_hor(get_image_array(img))
img_flip_hor= Image(5, 5, bytearray(img_array))
return img_flip_hor
def get_imgarr_flip_vert(imgarray):
output_array = []
for i in range(4, -1, -1):
row = imgarray[i*5:(i+1)*5]
output_array += row
return output_array
def get_image_flip_vert(img):
img_array = get_imgarr_flip_vert(get_image_array(img))
img_flip_vert= Image(5, 5, bytearray(img_array))
return img_flip_vert
object_images = [
Image.MUSIC_CROTCHET,
Image.MUSIC_QUAVER,
Image.MUSIC_QUAVERS,
Image.PACMAN,
Image.ROLLERSKATE,
Image.TRIANGLE_LEFT,
Image.UMBRELLA,
Image.YES,
]
def flip_imagelist(image_list, transition_time=500):
for img in image_list:
img_flip_hor = get_image_flip_hor(img)
img_flip_vert = get_image_flip_vert(img)
img_seq = [img, img_flip_hor, img, img_flip_vert, img]
display.show(img_seq, delay=transition_time)
while True:
flip_imagelist(object_images, transition_time=200)
11.9. Rotating 270 degrees
Tasks
Complete the grid of the renumbering of the indices in the image array. Observe the pattern and use that to code range functions for each list comprehension for each row of pixels.
Tasks
Write code to rotate the duck 90 degrees anticlockwise and swap between the display of the duck and the flipped duck.
Write code to rotate the duck 90 degrees anticlockwise and swap between the display of the duck and the flipped duck.
from microbit import * def get_image_array(img): img_repr = repr(img) img_str = img_repr[7:-3] img_str = img_str.replace(":", "") img_array = [int(x) for x in img_str] return img_array def get_imgarr_rotate_270(imgarray): # 4, 9, 14, 19, 24;; 3, 8, 13, 18, 23. row0 = [imgarray[x] for x in range(4, 25, 5)] row1 = [imgarray[x] for x in range(3, 25, 5)] row2 = [imgarray[x] for x in range(2, 25, 5)] row3 = [imgarray[x] for x in range(1, 25, 5)] row4 = [imgarray[x] for x in range(0, 25, 5)] output_array = row0 + row1 + row2 + row3 + row4 return output_array img = Image.DUCK img_array = get_imgarr_rotate_270(get_image_array(img)) img270 = Image(5, 5, bytearray(img_array)) while True: display.show(img) sleep(300) display.show(img270) sleep(300)
11.10. Rotating 90 degrees clockwise
Tasks
Complete the grid of the renumbering of the indices in the image array. Observe the pattern and use that to code range functions for each list comprehension for each row of pixels.
Tasks
Write code to rotate the duck 90 degrees clockwise and swap between the display of the duck and the flipped duck.
Write code to rotate the duck 90 degrees clockwise and swap between the display of the duck and the flipped duck.
from microbit import *
def get_image_array(img):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_rotate_90(imgarray):
# 20,15,10,5,0;;21,16,11,6,1...
row0 = [imgarray[x] for x in range(20, -1, -5)]
row1 = [imgarray[x] for x in range(21, -1, -5)]
row2 = [imgarray[x] for x in range(22, -1, -5)]
row3 = [imgarray[x] for x in range(23, -1, -5)]
row4 = [imgarray[x] for x in range(24, -1, -5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
img = Image.DUCK
img_array = get_imgarr_rotate_90(get_image_array(img))
img90 = Image(5, 5, bytearray(img_array))
while True:
display.show(img)
sleep(300)
display.show(img90)
sleep(300)
11.11. Rotating 180 degrees
Tasks
Write a function to combine flipping to rotate an image 180 degrees.
Write code to rotate the duck 180 degrees and swap between the display of the duck and the flipped duck.
from microbit import *
def get_imgarr_rotate_180(imgarray):
return get_imgarr_flip_vert(get_imgarr_flip_hor(imgarray))
from microbit import *
def get_image_array(img=Image.DUCK):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_flip_hor(imgarray):
# get every 5 elements and reverse them in each row
row0 = imgarray[:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_flip_vert(imgarray):
# get every 5 elements as rows and reverse order of rows.
row0 = imgarray[:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
def get_imgarr_rotate_180(imgarray):
return get_imgarr_flip_vert(get_imgarr_flip_hor(imgarray))
img = Image.DUCK
img180 = Image(5, 5, bytearray(get_imgarr_rotate_180(get_image_array(img))))
while True:
display.show(img)
sleep(800)
display.show(img180)
sleep(800)
11.12. Rotating image animation
Tasks
Use the functions developed on this page to create an animation of a duck rotating clockwise.
Use the functions developed on this page to create an animation of a duck rotating anti clockwise.
Use the functions developed on this page to create an animation of a duck rotating clockwise.
from microbit import *
def get_image_array(img=Image.DUCK):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_flip_hor(imgarray):
# get every 5 elements and reverse them in each row
row0 = imgarray[:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_flip_vert(imgarray):
# get every 5 elements as rows and reverse order of rows.
row0 = imgarray[:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
def get_imgarr_rotate_270(imgarray):
# 4, 9, 14, 19, 24;; 3, 8, 13, 18, 23.
row0 = [imgarray[x] for x in range(4, 25, 5)]
row1 = [imgarray[x] for x in range(3, 25, 5)]
row2 = [imgarray[x] for x in range(2, 25, 5)]
row3 = [imgarray[x] for x in range(1, 25, 5)]
row4 = [imgarray[x] for x in range(0, 25, 5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_rotate_90(imgarray):
# 20,15,10,5,0;;21,16,11,6,1...
row0 = [imgarray[x] for x in range(20, -1, -5)]
row1 = [imgarray[x] for x in range(21, -1, -5)]
row2 = [imgarray[x] for x in range(22, -1, -5)]
row3 = [imgarray[x] for x in range(23, -1, -5)]
row4 = [imgarray[x] for x in range(24, -1, -5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_rotate_180(imgarray):
return get_imgarr_flip_vert(get_imgarr_flip_hor(imgarray))
img0 = Image.DUCK
img90 = Image(5, 5, bytearray(get_imgarr_rotate_90(get_image_array(img0))))
img180 = Image(5, 5, bytearray(get_imgarr_rotate_180(get_image_array(img0))))
img270 = Image(5, 5, bytearray(get_imgarr_rotate_270(get_image_array(img0))))
img_seq = [img0, img90, img180, img270]
while True:
display.show(img_seq, delay=400)
Use the functions developed on this page to create an animation of a duck rotating anti clockwise.
from microbit import *
def get_image_array(img=Image.DUCK):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_flip_hor(imgarray):
# get every 5 elements and reverse them in each row
row0 = imgarray[:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_flip_vert(imgarray):
# get every 5 elements as rows and reverse order of rows.
row0 = imgarray[:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
def get_imgarr_rotate_270(imgarray):
# 4, 9, 14, 19, 24;; 3, 8, 13, 18, 23.
row0 = [imgarray[x] for x in range(4, 25, 5)]
row1 = [imgarray[x] for x in range(3, 25, 5)]
row2 = [imgarray[x] for x in range(2, 25, 5)]
row3 = [imgarray[x] for x in range(1, 25, 5)]
row4 = [imgarray[x] for x in range(0, 25, 5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_rotate_90(imgarray):
# 20,15,10,5,0;;21,16,11,6,1...
row0 = [imgarray[x] for x in range(20, -1, -5)]
row1 = [imgarray[x] for x in range(21, -1, -5)]
row2 = [imgarray[x] for x in range(22, -1, -5)]
row3 = [imgarray[x] for x in range(23, -1, -5)]
row4 = [imgarray[x] for x in range(24, -1, -5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_rotate_180(imgarray):
return get_imgarr_flip_vert(get_imgarr_flip_hor(imgarray))
img0 = Image.DUCK
img90 = Image(5, 5, bytearray(get_imgarr_rotate_90(get_image_array(img0))))
img180 = Image(5, 5, bytearray(get_imgarr_rotate_180(get_image_array(img0))))
img270 = Image(5, 5, bytearray(get_imgarr_rotate_270(get_image_array(img0))))
img_seq = [img0, img270, img180, img90]
while True:
display.show(img_seq, delay=400)
11.13. Rotating a list of images
Tasks
Write code to rotate a list of images using rotate_imagelist(image_list, transition_time=500).
Write code to rotate a list of images using rotate_imagelist(image_list, transition_time=500).
from microbit import *
def get_image_array(img=Image.DUCK):
img_repr = repr(img)
img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
return img_array
def get_imgarr_flip_hor(imgarray):
# get every 5 elements and reverse them in each row
row0 = imgarray[:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_flip_vert(imgarray):
# get every 5 elements as rows and reverse order of rows.
row0 = imgarray[:5]
row1 = imgarray[5:10]
row2 = imgarray[10:15]
row3 = imgarray[15:20]
row4 = imgarray[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
def get_imgarr_rotate_270(imgarray):
# 4, 9, 14, 19, 24;; 3, 8, 13, 18, 23.
row0 = [imgarray[x] for x in range(4, 25, 5)]
row1 = [imgarray[x] for x in range(3, 25, 5)]
row2 = [imgarray[x] for x in range(2, 25, 5)]
row3 = [imgarray[x] for x in range(1, 25, 5)]
row4 = [imgarray[x] for x in range(0, 25, 5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_rotate_90(imgarray):
# 20,15,10,5,0;;21,16,11,6,1...
row0 = [imgarray[x] for x in range(20, -1, -5)]
row1 = [imgarray[x] for x in range(21, -1, -5)]
row2 = [imgarray[x] for x in range(22, -1, -5)]
row3 = [imgarray[x] for x in range(23, -1, -5)]
row4 = [imgarray[x] for x in range(24, -1, -5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_imgarr_rotate_180(imgarray):
return get_imgarr_flip_vert(get_imgarr_flip_hor(imgarray))
animal_images = [
Image.RABBIT,
Image.COW,
Image.DUCK,
Image.TORTOISE,
Image.BUTTERFLY,
Image.GIRAFFE,
Image.SNAKE,
]
def rotate_imagelist(image_list, transition_time=500):
for img in image_list:
img0 = img
img180 = Image(5, 5, bytearray(get_imgarr_rotate_180(get_image_array(img0))))
img270 = Image(5, 5, bytearray(get_imgarr_rotate_270(get_image_array(img0))))
img90 = Image(5, 5, bytearray(get_imgarr_rotate_90(get_image_array(img0))))
img_seq = [img0, img90, img180, img270]
display.show(img_seq, delay=transition_time)
while True:
rotate_imagelist(animal_images)