12. Flip and Rotate images
12.1. Useful Syntax
12.1.1. Image repr
- repr(image)
- Get a compact string representation of the image.
12.1.2. String replace
- string.replace(old_value, new_value, count)
- old_value The string to search fornew_value 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.
12.1.3. Reverse list method syntax
- a_list.reverse()
- Reverses a list. No parameters are involved.
12.2. Pixels from repr
from microbit import *
img0 = Image.DUCK
img_repr = repr(img0)
print(img_repr)
# Image('09900:99900:09999:09990:00000:')
12.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]
Tasks
Write a function to return and image array from an image.
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
12.4. So far
flipping horizontally
flipping vertically
rotating 90 degrees clockwise or 90 anticlockwise.
12.5. Flipping horizontally
![../_images/flipping_horizontally.png](../_images/flipping_horizontally.png)
Tasks
Write code to flip the duck horizontally and swap between the display of the duck and the flipped duck.
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_img_arr_flip_hor(img_array):
# get every 5 elements and reverse them
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
img0 = Image.DUCK
img_array = get_img_arr_flip_hor(get_image_array(img0))
img0_flip_hor= Image(5, 5, bytearray(img_array))
while True:
display.show(img0)
sleep(300)
display.show(img0_flip_hor)
sleep(300)
12.6. Flipping vertically
![../_images/flipping_vertically.png](../_images/flipping_vertically.png)
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_img_arr_flip_vert(img_array):
# get every 5 elements as rows and reverse order of rows.
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
img0 = Image.DUCK
img_array = get_img_arr_flip_vert(get_image_array(img0))
img0_flip_vert = Image(5, 5, bytearray(img_array))
while True:
display.show(img0)
sleep(300)
display.show(img0_flip_vert)
sleep(300)
12.7. Flipping a list of images
Tasks
Write code to rotate a list of images using flip_image_list(image_list, transition_time=300).
Write code to rotate a list of images using rotate_image_list(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_img_arr_flip_hor(img_array):
# get every 5 elements and reverse them in each row
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_flip_vert(img_array):
# get every 5 elements as rows and reverse order of rows.
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
object_images = [
Image.MUSIC_CROTCHET,
Image.MUSIC_QUAVER,
Image.MUSIC_QUAVERS,
Image.PACMAN,
Image.ROLLERSKATE,
Image.TRIANGLE_LEFT,
Image.UMBRELLA,
Image.YES,
]
def flip_image_list(image_list, transition_time=500):
for img in image_list:
img0 = img
img_flip_hor = Image(5, 5, bytearray(get_img_arr_flip_hor(get_image_array(img0))))
img_flip_vert = Image(5, 5, bytearray(get_img_arr_flip_vert(get_image_array(img0))))
img_seq = [img0, img_flip_hor, img0, img_flip_vert, img0]
display.show(img_seq, delay=transition_time)
while True:
flip_image_list(object_images)
12.8. Rotating 270 degrees
![../_images/rotating_270.png](../_images/rotating_270.png)
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.
![../_images/rotate_270_arrayQ.png](../_images/rotate_270_arrayQ.png)
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_img_arr_rotate_270(img_array):
# 4, 9, 14, 19, 24;; 3, 8, 13, 18, 23.
row0 = [img_array[x] for x in range(4, 25, 5)]
row1 = [img_array[x] for x in range(3, 25, 5)]
row2 = [img_array[x] for x in range(2, 25, 5)]
row3 = [img_array[x] for x in range(1, 25, 5)]
row4 = [img_array[x] for x in range(0, 25, 5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
img0 = Image.DUCK
img_array = get_img_arr_rotate_270(get_image_array(img0))
img270 = Image(5, 5, bytearray(img_array))
while True:
display.show(img0)
sleep(300)
display.show(img270)
sleep(300)
12.9. Rotating 90 degrees clockwise
![../_images/rotating_90.png](../_images/rotating_90.png)
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.
![../_images/rotate_90_arrayQ.png](../_images/rotate_90_arrayQ.png)
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_img_arr_rotate_90(img_array):
# 20,15,10,5,0;;21,16,11,6,1...
row0 = [img_array[x] for x in range(20, -1, -5)]
row1 = [img_array[x] for x in range(21, -1, -5)]
row2 = [img_array[x] for x in range(22, -1, -5)]
row3 = [img_array[x] for x in range(23, -1, -5)]
row4 = [img_array[x] for x in range(24, -1, -5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
img0 = Image.DUCK
img_array = get_img_arr_rotate_90(get_image_array(img0))
img90 = Image(5, 5, bytearray(img_array))
while True:
display.show(img0)
sleep(300)
display.show(img90)
sleep(300)
12.10. 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_img_arr_rotate_180(img_array):
return get_img_arr_flip_vert(get_img_arr_flip_hor(img_array))
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_img_arr_flip_hor(img_array):
# get every 5 elements and reverse them in each row
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_flip_vert(img_array):
# get every 5 elements as rows and reverse order of rows.
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
def get_img_arr_rotate_180(img_array):
return get_img_arr_flip_vert(get_img_arr_flip_hor(img_array))
img0 = Image.DUCK
img180 = Image(5, 5, bytearray(get_img_arr_rotate_180(get_image_array(img0))))
while True:
display.show(img0)
sleep(800)
display.show(img180)
sleep(800)
12.11. Rotating image animation
![../_images/duck_clockwise.gif](../_images/duck_clockwise.gif)
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_img_arr_flip_hor(img_array):
# get every 5 elements and reverse them in each row
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_flip_vert(img_array):
# get every 5 elements as rows and reverse order of rows.
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
def get_img_arr_rotate_270(img_array):
# 4, 9, 14, 19, 24;; 3, 8, 13, 18, 23.
row0 = [img_array[x] for x in range(4, 25, 5)]
row1 = [img_array[x] for x in range(3, 25, 5)]
row2 = [img_array[x] for x in range(2, 25, 5)]
row3 = [img_array[x] for x in range(1, 25, 5)]
row4 = [img_array[x] for x in range(0, 25, 5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_rotate_90(img_array):
# 20,15,10,5,0;;21,16,11,6,1...
row0 = [img_array[x] for x in range(20, -1, -5)]
row1 = [img_array[x] for x in range(21, -1, -5)]
row2 = [img_array[x] for x in range(22, -1, -5)]
row3 = [img_array[x] for x in range(23, -1, -5)]
row4 = [img_array[x] for x in range(24, -1, -5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_rotate_180(img_array):
return get_img_arr_flip_vert(get_img_arr_flip_hor(img_array))
img0 = Image.DUCK
img90 = Image(5, 5, bytearray(get_img_arr_rotate_90(get_image_array(img0))))
img180 = Image(5, 5, bytearray(get_img_arr_rotate_180(get_image_array(img0))))
img270 = Image(5, 5, bytearray(get_img_arr_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_img_arr_flip_hor(img_array):
# get every 5 elements and reverse them in each row
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_flip_vert(img_array):
# get every 5 elements as rows and reverse order of rows.
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
def get_img_arr_rotate_270(img_array):
# 4, 9, 14, 19, 24;; 3, 8, 13, 18, 23.
row0 = [img_array[x] for x in range(4, 25, 5)]
row1 = [img_array[x] for x in range(3, 25, 5)]
row2 = [img_array[x] for x in range(2, 25, 5)]
row3 = [img_array[x] for x in range(1, 25, 5)]
row4 = [img_array[x] for x in range(0, 25, 5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_rotate_90(img_array):
# 20,15,10,5,0;;21,16,11,6,1...
row0 = [img_array[x] for x in range(20, -1, -5)]
row1 = [img_array[x] for x in range(21, -1, -5)]
row2 = [img_array[x] for x in range(22, -1, -5)]
row3 = [img_array[x] for x in range(23, -1, -5)]
row4 = [img_array[x] for x in range(24, -1, -5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_rotate_180(img_array):
return get_img_arr_flip_vert(get_img_arr_flip_hor(img_array))
img0 = Image.DUCK
img90 = Image(5, 5, bytearray(get_img_arr_rotate_90(get_image_array(img0))))
img180 = Image(5, 5, bytearray(get_img_arr_rotate_180(get_image_array(img0))))
img270 = Image(5, 5, bytearray(get_img_arr_rotate_270(get_image_array(img0))))
img_seq = [img0, img270, img180, img90]
while True:
display.show(img_seq, delay=400)
12.12. Rotating a list of images
Tasks
Write code to rotate a list of images using rotate_image_list(image_list, transition_time=500).
Write code to rotate a list of images using rotate_image_list(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_img_arr_flip_hor(img_array):
# get every 5 elements and reverse them in each row
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
row0.reverse()
row1.reverse()
row2.reverse()
row3.reverse()
row4.reverse()
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_flip_vert(img_array):
# get every 5 elements as rows and reverse order of rows.
row0 = img_array[:5]
row1 = img_array[5:10]
row2 = img_array[10:15]
row3 = img_array[15:20]
row4 = img_array[20:]
output_array = row4 + row3 + row2 + row1 + row0
return output_array
def get_img_arr_rotate_270(img_array):
# 4, 9, 14, 19, 24;; 3, 8, 13, 18, 23.
row0 = [img_array[x] for x in range(4, 25, 5)]
row1 = [img_array[x] for x in range(3, 25, 5)]
row2 = [img_array[x] for x in range(2, 25, 5)]
row3 = [img_array[x] for x in range(1, 25, 5)]
row4 = [img_array[x] for x in range(0, 25, 5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_rotate_90(img_array):
# 20,15,10,5,0;;21,16,11,6,1...
row0 = [img_array[x] for x in range(20, -1, -5)]
row1 = [img_array[x] for x in range(21, -1, -5)]
row2 = [img_array[x] for x in range(22, -1, -5)]
row3 = [img_array[x] for x in range(23, -1, -5)]
row4 = [img_array[x] for x in range(24, -1, -5)]
output_array = row0 + row1 + row2 + row3 + row4
return output_array
def get_img_arr_rotate_180(img_array):
return get_img_arr_flip_vert(get_img_arr_flip_hor(img_array))
animal_images = [
Image.RABBIT,
Image.COW,
Image.DUCK,
Image.TORTOISE,
Image.BUTTERFLY,
Image.GIRAFFE,
Image.SNAKE,
]
def rotate_image_list(image_list, transition_time=500):
for img in image_list:
img0 = img
img180 = Image(5, 5, bytearray(get_img_arr_rotate_180(get_image_array(img0))))
img270 = Image(5, 5, bytearray(get_img_arr_rotate_270(get_image_array(img0))))
img90 = Image(5, 5, bytearray(get_img_arr_rotate_90(get_image_array(img0))))
img_seq = [img0, img90, img180, img270]
display.show(img_seq, delay=transition_time)
while True:
rotate_image_list(animal_images)