11. Flip and Rotate images

Custom functions are needed to flip or rotate images.
The pixel brightness data needs to be obtained then re-organised and used to create a new image.

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

The replace method will be used to remove the colons in the image brightness string.
string.replace(oldvalue, newvalue, count)
oldvalue - The string to search for
newvalue - The string to replace the old value with
count (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

To get the pixel data, use the repr function on the image object.
In the code example below, the repr for the DUCK image is printed.
Image(‘09900:99900:09999:09990:00000:’)
from microbit import *

img = Image.DUCK
img_repr = repr(img)
print(img_repr)
# Image('09900:99900:09999:09990:00000:')

11.3. Brightness array

The next step is to collect just the numbers from the string, then put the numbers in a list format that can then be used to create an image using bytearray.
So: Image(‘09900:99900:09999:09990:00000:’)
is converted to: [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]
from microbit import *

img_str = img_repr[7:-3]
img_str = img_str.replace(":", "")
img_array = [int(x) for x in img_str]
The string can be sliced to ignore the first 6 characters and the last 3 characters
This is done using img_repr[7:-3].
Then the colon is removed using the replace method. img_str = img_str.replace(“:”, “”)
Finally, list comprehension, img_array = [int(x) for x in img_str], is used on the string to convert each string numeral to an int in a list.
This produces the list for the Image of a DUCK:
[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]
This list comprehension is short for a for-loop like this:
from microbit import *

img_array = []
for x in img_str:
    img_array.append(int(x))

Tasks

  1. 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

Now, functions need to be created for:
  • flipping horizontally

  • flipping vertically

  • rotating 90 degrees clockwise or 90 anticlockwise.


11.5. Flipping horizontally

../_images/flipping_horizontally.png
The code to flip an image horizontally will be broken up into custom functions.
get_image_flip_hor(img) takes the original image and returns the flipped image.
get_image_array(img) takes an image object as an argument and returns a list of pixel brightnesses.
get_imgarr_flip_hor(imgarray) takes the image array returned by get_image_array and outputs a flipped image array.
get_imgarr_flip_hor(imgarray) should use list slices to get each row.
The top row would be the first 5 items of the list as given by: row0 = imgarray[:5]
Each row slice can be reversed: row0.reverse()
Image(5, 5, bytearray(img_array)) creates the flipped image.

Tasks

  1. 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)
In the previous answer, notice the pattern in each slice can be replaced with a loop using imgarray[i:i+5].
Use the range function to set i to multiples of 5: for i in range(0, len(imgarray), 5)

Tasks

  1. 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
Full code:
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

get_imgarr_flip_hor can also be done via list comprehension usinga helper function to do the list reversal.
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

../_images/flipping_vertically.png
Add a new function to flip an image vertically using the image array.
get_imgarr_flip_vert(imgarray) takes the image array returned by get_image_array and outputs a flipped image array.
get_imgarr_flip_vert(imgarray) should use list slices to get each row.
The top row would be the first 5 items of the list as given by: row0 = imgarray[:5]
The order of each row needs to be reversed, so that the top row goes to the bottom row.
Here is the get_imgarr_flip_vert code.
The get_imgarr_flip_vert code can be modified to sue a for-loop by adding row 4 to the returned array, then row 3 down to row 0.

Tasks

  1. 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

  1. 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

Here is the list of some images that are not laterally symmetrical (left and right sides are diferent):
[Image.MUSIC_CROTCHET, Image.MUSIC_QUAVER, Image.MUSIC_QUAVERS, Image.PACMAN, Image.ROLLERSKATE, Image.TRIANGLE_LEFT, Image.UMBRELLA, Image.YES]
Create a function, flip_imagelist(image_list, transition_time=500), which creates img, img_flip_hor and img_flip_vert for each image in the list, then creates a list of these, [img, img_flip_hor, img, img_flip_vert, img] which are then displayed using a transition_time which defaults to a delay of 300ms between each image.

Tasks

  1. 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

../_images/rotating_270.png
Rotating 90 degrees anticlockwise is the same as rotating 270 degrees clockwise.
Add a new function to rotate an image 90 degrees anticlockwise using the image array.
get_imgarr_rotate_270(imgarray) takes the image array returned by get_image_array and outputs a rotated image array.
get_imgarr_rotate_270(imgarray) should use list comprehensions to get each row.
The top row is made up of pixels that had previous array indices of 4, 9, 14, 19, 24.
This sequence can be created with the range function.
A similar pattern occurs for the others rows.

Tasks

  1. 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

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_array.png

Tasks

  1. 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

../_images/rotating_90.png
Add a new function to rotate an image 90 degrees anticlockwise using the image array.
get_imgarr_rotate_90(imgarray) takes the image array returned by get_image_array and outputs a rotated image array.
get_imgarr_rotate_90(imgarray) should use list comprehensions to get each row.
The top row is made up of pixels that had previous array indices of 20, 15, 10, 5, 0.
This sequence can be created with the range function.
A similar pattern occurs for the others rows.

Tasks

  1. 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

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_array.png

Tasks

  1. 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

Rotating 180 degrees can be achieved by combine flipping horizontally with flipping vertically.
get_imgarr_rotate_180(imgarray) takes the image array returned by get_image_array and outputs a rotated image array.
get_imgarr_rotate_180(imgarray) combines the flipping functions.

Tasks

  1. Write a function to combine flipping to rotate an image 180 degrees.

  2. 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

../_images/duck_clockwise.gif

Tasks

  1. Use the functions developed on this page to create an animation of a duck rotating clockwise.

  2. 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

Here is the list of animals:
[Image.RABBIT, Image.COW, Image.DUCK, Image.TORTOISE, Image.BUTTERFLY, Image.GIRAFFE, Image.SNAKE]
Create a function, rotate_imagelist(image_list, transition_time=500), which creates img0, img90, img180 and img270 for each image in the list, then creates a list of these which are then displayed using a transition_time which defaults to 500ms.

Tasks

  1. 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)