Skip to content

Commit 981a38b

Browse files
authored
Merge pull request #928 from pimoroni/examples/pngdec
Examples/pngdec
2 parents 25786de + f962c3c commit 981a38b

File tree

10 files changed

+232
-0
lines changed

10 files changed

+232
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from picographics import PicoGraphics, DISPLAY_INKY_FRAME as DISPLAY # 5.7"
2+
# from picographics import PicoGraphics, DISPLAY_INKY_FRAME_4 as DISPLAY # 4.0"
3+
# from picographics import PicoGraphics, DISPLAY_INKY_FRAME_7 as DISPLAY # 7.3"
4+
import pngdec
5+
6+
# Create a PicoGraphics instance
7+
graphics = PicoGraphics(DISPLAY)
8+
WIDTH, HEIGHT = graphics.get_bounds()
9+
10+
# Set the font
11+
graphics.set_font("bitmap8")
12+
13+
# Create an instance of the PNG Decoder
14+
png = pngdec.PNG(graphics)
15+
16+
# Clear the screen
17+
graphics.set_pen(1)
18+
graphics.clear()
19+
graphics.set_pen(0)
20+
21+
# Few lines of text.
22+
graphics.text("PNG Pencil", 70, 100, WIDTH, 3)
23+
24+
# Open our PNG File from flash. In this example we're using a cartoon pencil.
25+
# You can use Thonny to transfer PNG Images to your Inky Frame.
26+
try:
27+
png.open_file("pencil_256x256.png")
28+
29+
# Decode our PNG file and set the X and Y
30+
png.decode(200, 100)
31+
32+
except OSError:
33+
graphics.text("Unable to find PNG file! Copy 'pencil_256x256.png' to your Inky Frame using Thonny :)", 10, 70, WIDTH, 3)
34+
35+
# Start the screen update
36+
graphics.update()
5.41 KB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_RGB332
2+
import pngdec
3+
4+
# Create a PicoGraphics instance
5+
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB332)
6+
7+
# Set the backlight so we can see it!
8+
display.set_backlight(1.0)
9+
10+
# Create an instance of the PNG Decoder
11+
png = pngdec.PNG(display)
12+
13+
# Create some pens for use later.
14+
BG = display.create_pen(200, 200, 200)
15+
TEXT = display.create_pen(0, 0, 0)
16+
17+
# Clear the screen
18+
display.set_pen(BG)
19+
display.clear()
20+
21+
display.set_pen(TEXT)
22+
display.text("PNG Pencil", 15, 80)
23+
24+
try:
25+
# Open our PNG File from flash. In this example we're using an image of a cartoon pencil.
26+
# You can use Thonny to transfer PNG Images to your Pico.
27+
png.open_file("pencil.png")
28+
29+
# Decode our PNG file and set the X and Y
30+
png.decode(20, 100, scale=3)
31+
32+
# Handle the error if the image doesn't exist on the flash.
33+
except OSError:
34+
print("Error: PNG File missing. Copy the PNG file from the example folder to your Pico using Thonny and run the example again.")
35+
36+
display.update()
37+
38+
# We're not doing anything else with the display now but we want to keep the program running!
39+
while True:
40+
pass
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_P8
2+
import pngdec
3+
4+
# Create a PicoGraphics instance
5+
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P8, rotate=270)
6+
7+
# Get the display width/height so we can position the PNGs
8+
width, height = display.get_bounds()
9+
10+
# Set the backlight so we can see it!
11+
display.set_backlight(1.0)
12+
13+
# Create an instance of the PNG Decoder
14+
png = pngdec.PNG(display)
15+
16+
# Create some pens for use later.
17+
BG = display.create_pen(200, 200, 200)
18+
19+
# 16 Reds
20+
for i in range(16):
21+
display.create_pen(i * 16, 0, 0)
22+
23+
# 16 Greens
24+
for i in range(16):
25+
display.create_pen(0, i * 16, 0)
26+
27+
# 16 Blues
28+
for i in range(16):
29+
display.create_pen(0, 0, i * 16)
30+
31+
# Adding in an white background colour at the beginning of each offset.
32+
for i in range(3):
33+
display.update_pen(i * 16, 200, 200, 200)
34+
35+
# Clear the screen
36+
display.set_pen(BG)
37+
display.clear()
38+
39+
40+
try:
41+
# Open our PNG File from flash. In this example we're using an image of a cartoon pencil.
42+
# You can use Thonny to transfer PNG Images to your Pico.
43+
png.open_file("pencil_gray.png")
44+
45+
# Horizontally/vertically center the three PNG Images.
46+
png_w = png.get_width()
47+
png_h = png.get_height()
48+
49+
offset_x = (width - png_w * 2) // 2
50+
height_y = (height // 3)
51+
offset_y = (height_y - png_h * 2) // 2
52+
53+
# Decode our PNG file and set the X and Y
54+
png.decode(offset_x, offset_y, scale=2, mode=pngdec.PNG_COPY, palette_offset=0)
55+
png.decode(offset_x, offset_y + height_y, scale=2, mode=pngdec.PNG_COPY, palette_offset=16)
56+
png.decode(offset_x, offset_y + (height_y * 2), scale=2, mode=pngdec.PNG_COPY, palette_offset=32)
57+
58+
# Handle the error if the image doesn't exist on the flash.
59+
except OSError:
60+
print("Error: PNG File missing. Copy the PNG file from the example folder to your Pico using Thonny and run the example again.")
61+
62+
display.update()
63+
64+
# We're not doing anything else with the display now but we want to keep the program running!
65+
while True:
66+
pass
1.09 KB
Loading
497 Bytes
Loading
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from picographics import PicoGraphics, DISPLAY_TUFTY_2040
2+
import pngdec
3+
4+
display = PicoGraphics(display=DISPLAY_TUFTY_2040)
5+
6+
# Create an instance of the PNG Decoder
7+
png = pngdec.PNG(display)
8+
9+
# Create some pens for use later.
10+
BG = display.create_pen(200, 200, 200)
11+
TEXT = display.create_pen(0, 0, 0)
12+
13+
# Clear the screen
14+
display.set_pen(BG)
15+
display.clear()
16+
17+
display.set_pen(TEXT)
18+
display.text("PNG Pencil", 105, 80)
19+
20+
try:
21+
# Open our PNG File from flash. In this example we're using an image of a cartoon pencil.
22+
# You can use Thonny to transfer PNG Images to your Pico.
23+
png.open_file("pencil.png")
24+
25+
# Decode our PNG file and set the X and Y
26+
png.decode(140, 100)
27+
28+
# Handle the error if the image doesn't exist on the flash.
29+
except OSError:
30+
print("Error: PNG File missing. Copy the PNG file from the example folder to your Pico using Thonny and run the example again.")
31+
32+
display.update()
33+
34+
# We're not doing anything else with the display now but we want to keep the program running!
35+
while True:
36+
pass
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_P8
2+
import pngdec
3+
4+
display = PicoGraphics(display=DISPLAY_TUFTY_2040, pen_type=PEN_P8)
5+
6+
# Create an instance of the PNG Decoder
7+
png = pngdec.PNG(display)
8+
9+
# Create some pens for use later.
10+
BG = display.create_pen(200, 200, 200)
11+
TEXT = display.create_pen(0, 0, 0)
12+
13+
# 16 Reds
14+
for i in range(15):
15+
display.create_pen(i * 16, 0, 0)
16+
17+
# 16 Greens
18+
for i in range(16):
19+
display.create_pen(0, i * 16, 0)
20+
21+
# 16 Blues
22+
for i in range(15):
23+
display.create_pen(0, 0, i * 16)
24+
25+
# Adding in an white background colour at the beginning of each offset.
26+
for i in range(3):
27+
display.update_pen(i * 16, 200, 200, 200)
28+
29+
# Clear the screen
30+
display.set_pen(BG)
31+
display.clear()
32+
33+
display.set_pen(TEXT)
34+
display.text("PNG Pencil \n& Offset Palette", 125, 115)
35+
36+
try:
37+
# Open our PNG File from flash. In this example we're using an image of a cartoon pencil.
38+
# You can use Thonny to transfer PNG Images to your Pico.
39+
png.open_file("pencil_gray.png")
40+
41+
# Decode our PNG file and set the X and Y
42+
png.decode(35, 10, scale=2, mode=pngdec.PNG_COPY, palette_offset=0)
43+
png.decode(35, 90, scale=2, mode=pngdec.PNG_COPY, palette_offset=16)
44+
png.decode(35, 170, scale=2, mode=pngdec.PNG_COPY, palette_offset=32)
45+
46+
# Handle the error if the image doesn't exist on the flash.
47+
except OSError:
48+
print("Error: PNG File missing. Copy the PNG file from the example folder to your Pico using Thonny and run the example again.")
49+
50+
display.update()
51+
52+
# We're not doing anything else with the display now but we want to keep the program running!
53+
while True:
54+
pass
1.09 KB
Loading
497 Bytes
Loading

0 commit comments

Comments
 (0)