|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2022 Tim Cocks |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +Use bitmaptools.rotozoom() to scale a bmp image and show multiple sizes of it on the display. |
| 6 | +""" |
| 7 | +importboard |
| 8 | +importadafruit_imageload |
| 9 | +importbitmaptools |
| 10 | +importdisplayio |
| 11 | + |
| 12 | +# use the builtin display |
| 13 | +display=board.DISPLAY |
| 14 | + |
| 15 | +# load bmp image into a Bitmap and Palette objects |
| 16 | +source_bitmap, source_palette=adafruit_imageload.load( |
| 17 | +"Billie.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette |
| 18 | +) |
| 19 | + |
| 20 | +# Create destination Bitmap object to hold the rotated image |
| 21 | +dest_bitmap=displayio.Bitmap( |
| 22 | +source_bitmap.height*2, source_bitmap.height*2, len(source_palette) |
| 23 | +) |
| 24 | + |
| 25 | +# Create a TileGrid to show the destination Bitmap with the rotated image in it |
| 26 | +dest_tile_grid=displayio.TileGrid(dest_bitmap, pixel_shader=source_palette) |
| 27 | + |
| 28 | +# rotozoom() takes from source bitmap, puts into destination bitmap. |
| 29 | +# clipping is not used so whole source image is put into the destination bitmap. |
| 30 | +# scale argument accepts a float number that will multiply the size of the source bitmap times |
| 31 | +# the scale factor. |
| 32 | +INITIAL_SCALE=0.5 |
| 33 | +bitmaptools.rotozoom(dest_bitmap, source_bitmap, scale=INITIAL_SCALE) |
| 34 | + |
| 35 | +# move the destination image out of the corner a little |
| 36 | +dest_tile_grid.x=10 |
| 37 | +dest_tile_grid.y=10 |
| 38 | + |
| 39 | +# Create a Group to show the TileGrid |
| 40 | +group=displayio.Group() |
| 41 | + |
| 42 | +# Add the TileGrid to the Group |
| 43 | +group.append(dest_tile_grid) |
| 44 | + |
| 45 | +# Add the Group to the Display |
| 46 | +display.show(group) |
| 47 | + |
| 48 | +# we will manually refresh the display only after we make changes to our destination bitmap |
| 49 | +display.auto_refresh=False |
| 50 | + |
| 51 | +# Loop forever |
| 52 | +whileTrue: |
| 53 | +# loop from 0.5 to 2.0 scale factors. step size 0.1 |
| 54 | +forscaleinrange(5, 21): |
| 55 | +# empty the destination bitmap, clear out the previously drawn frame |
| 56 | +dest_bitmap.fill(0) |
| 57 | + |
| 58 | +# paste in billie at the current scale size from our loop |
| 59 | +bitmaptools.rotozoom(dest_bitmap, source_bitmap, scale=scale/10) |
| 60 | + |
| 61 | +# refresh the display to draw our changes |
| 62 | +display.refresh() |
| 63 | + |
| 64 | +# loop from 2.0 to 0.5 scale factors. step size 0.1 |
| 65 | +forscaleinrange(20, 4, -1): |
| 66 | +# empty the destination bitmap, clear out the previously drawn frame |
| 67 | +dest_bitmap.fill(0) |
| 68 | + |
| 69 | +# paste in billie at the current scale size from our loop |
| 70 | +bitmaptools.rotozoom(dest_bitmap, source_bitmap, scale=scale/10) |
| 71 | + |
| 72 | +# refresh the display to draw our changes |
| 73 | +display.refresh() |
0 commit comments