Skip to content

Commit d33140d

Browse files
committed
add scale animated example. format rotate and scale examples.
1 parent 172a1af commit d33140d

5 files changed

+122
-35
lines changed

bitmaptools/Billie.bmp.license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2018 Adafruit Industries
22
#
3-
# SPDX-License-Identifier: MIT
3+
# SPDX-License-Identifier: MIT

bitmaptools/bitmaptools_rotate_bmp.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@
55
Use bitmaptools.rotozoom() to rotate a bmp image.
66
"""
77
import math
8-
import bitmaptools
98
import board
10-
import displayio
119
import adafruit_imageload
10+
import displayio
11+
import bitmaptools
1212

1313
# use the builtin display
1414
display = board.DISPLAY
1515

1616
# load bmp image into a Bitmap and Palette objects
17-
source_bitmap, source_palette = adafruit_imageload.load("Billie.bmp",
18-
bitmap=displayio.Bitmap,
19-
palette=displayio.Palette)
17+
source_bitmap, source_palette = adafruit_imageload.load(
18+
"Billie.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
19+
)
2020
# Create a TileGrid to show the bitmap
2121
source_tile_grid = displayio.TileGrid(source_bitmap, pixel_shader=source_palette)
2222

2323
# Create destination Bitmap object to hold the rotated image
24-
dest_bitmap = displayio.Bitmap(source_bitmap.height, source_bitmap.height, len(source_palette))
24+
dest_bitmap = displayio.Bitmap(
25+
source_bitmap.height, source_bitmap.height, len(source_palette)
26+
)
2527

2628
# Create a TileGrid to show the destination Bitmap with the rotated image in it
2729
dest_tile_grid = displayio.TileGrid(dest_bitmap, pixel_shader=source_palette)

bitmaptools/bitmaptools_rotate_bmp_animated.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@
55
Use bitmaptools.rotozoom() to rotate a bmp image in a circle animated.
66
"""
77
import math
8-
import bitmaptools
98
import board
10-
import displayio
119
import adafruit_imageload
10+
import bitmaptools
11+
import displayio
1212

1313
# use the builtin display
1414
display = board.DISPLAY
1515

1616
# load bmp image into a Bitmap and Palette objects
17-
source_bitmap, source_palette = adafruit_imageload.load("Billie.bmp",
18-
bitmap=displayio.Bitmap,
19-
palette=displayio.Palette)
17+
source_bitmap, source_palette = adafruit_imageload.load(
18+
"Billie.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
19+
)
2020
# Create a TileGrid to show the bitmap
2121
source_tile_grid = displayio.TileGrid(source_bitmap, pixel_shader=source_palette)
2222

2323
# Create destination Bitmap object to hold the rotated image
24-
dest_bitmap = displayio.Bitmap(source_bitmap.height, source_bitmap.height, len(source_palette))
24+
dest_bitmap = displayio.Bitmap(
25+
source_bitmap.height, source_bitmap.height, len(source_palette)
26+
)
2527

2628
# Create a TileGrid to show the destination Bitmap with the rotated image in it
2729
dest_tile_grid = displayio.TileGrid(dest_bitmap, pixel_shader=source_palette)

bitmaptools/bitmaptools_scale_bmp.py

+32-22
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44
"""
55
Use bitmaptools.rotozoom() to scale a bmp image and show multiple sizes of it on the display.
66
"""
7-
import bitmaptools
87
import board
9-
import displayio
108
import adafruit_imageload
9+
import bitmaptools
10+
import displayio
1111

1212
# use the builtin display
1313
display = board.DISPLAY
1414

1515
# load bmp image into a Bitmap and Palette objects
16-
source_bitmap, source_palette = adafruit_imageload.load("Billie.bmp",
17-
bitmap=displayio.Bitmap,
18-
palette=displayio.Palette)
16+
source_bitmap, source_palette = adafruit_imageload.load(
17+
"Billie.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
18+
)
1919

2020
# Create destination Bitmap object to hold the rotated image
21-
dest_bitmap = displayio.Bitmap(source_bitmap.height * 2, source_bitmap.height * 2,
22-
len(source_palette))
21+
dest_bitmap = displayio.Bitmap(
22+
source_bitmap.height * 2, source_bitmap.height * 2, len(source_palette)
23+
)
2324

2425
# Create a TileGrid to show the destination Bitmap with the rotated image in it
2526
dest_tile_grid = displayio.TileGrid(dest_bitmap, pixel_shader=source_palette)
@@ -30,25 +31,34 @@
3031
# the scale factor.
3132

3233
# Big Billie
33-
big_billie_scale = 1.75
34-
bitmaptools.rotozoom(dest_bitmap, source_bitmap,
35-
ox=int(dest_bitmap.width - ((source_bitmap.width / 2) * big_billie_scale)),
36-
oy=int((source_bitmap.height / 2) * big_billie_scale),
37-
scale=big_billie_scale)
34+
BIG_BILLIE_SCALE = 1.75
35+
bitmaptools.rotozoom(
36+
dest_bitmap,
37+
source_bitmap,
38+
ox=int(dest_bitmap.width - ((source_bitmap.width / 2) * BIG_BILLIE_SCALE)),
39+
oy=int((source_bitmap.height / 2) * BIG_BILLIE_SCALE),
40+
scale=BIG_BILLIE_SCALE,
41+
)
3842

3943
# Normal sized Billie
40-
normal_billie_scale = 1.0
41-
bitmaptools.rotozoom(dest_bitmap, source_bitmap,
42-
ox=int(dest_bitmap.width/2) - 24,
43-
oy=int((source_bitmap.height / 2) * normal_billie_scale),
44-
scale=normal_billie_scale)
44+
NORMAL_BILLIE_SCALE = 1.0
45+
bitmaptools.rotozoom(
46+
dest_bitmap,
47+
source_bitmap,
48+
ox=int(dest_bitmap.width / 2) - 24,
49+
oy=int((source_bitmap.height / 2) * NORMAL_BILLIE_SCALE),
50+
scale=NORMAL_BILLIE_SCALE,
51+
)
4552

4653
# Little Billie
47-
little_billie_scale = 0.5
48-
bitmaptools.rotozoom(dest_bitmap, source_bitmap,
49-
ox=int((source_bitmap.width / 2) * little_billie_scale),
50-
oy=int((source_bitmap.height / 2) * little_billie_scale),
51-
scale=little_billie_scale)
54+
LITTLE_BILLY_SCALE = 0.5
55+
bitmaptools.rotozoom(
56+
dest_bitmap,
57+
source_bitmap,
58+
ox=int((source_bitmap.width / 2) * LITTLE_BILLY_SCALE),
59+
oy=int((source_bitmap.height / 2) * LITTLE_BILLY_SCALE),
60+
scale=LITTLE_BILLY_SCALE,
61+
)
5262

5363
# move the destination image out of the corner a little
5464
dest_tile_grid.x = 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
import board
8+
import adafruit_imageload
9+
import bitmaptools
10+
import displayio
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+
while True:
53+
# loop from 0.5 to 2.0 scale factors. step size 0.1
54+
for scale in range(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+
for scale in range(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

Comments
 (0)