Skip to content

Commit 3d37304

Browse files
committed
Add the ability to display text with transparent background
1 parent 883dc7d commit 3d37304

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

main.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/env python3
2+
# A simple Python manager for "Turing Smart Screen" 3.5" IPS USB-C display
3+
# https://github.com/mathoudebine/turing-smart-screen-python
4+
25
import struct
36
from time import sleep
47
import serial # Install pyserial : pip install pyserial
@@ -11,6 +14,7 @@
1114
DISPLAY_WIDTH = 320
1215
DISPLAY_HEIGHT = 480
1316

17+
1418
class Command:
1519
RESET = 101
1620
CLEAR = 102
@@ -83,21 +87,26 @@ def DisplayText(ser, text, x=0, y=0,
8387
font="roboto/Roboto-Regular.ttf",
8488
font_size=20,
8589
font_color=(0, 0, 0),
86-
background_color=(255, 255, 255)):
90+
background_color=(255, 255, 255),
91+
background_image=None):
8792
# Convert text to bitmap using PIL and display it
93+
# Provide the background image path to display text with transparent background
8894

89-
# The text bitmap is created with max width/height by default
90-
# Note : alpha component from RGBA is not supported by the display
91-
text_image = Image.new('RGB', (DISPLAY_WIDTH, DISPLAY_HEIGHT), background_color)
95+
if background_image is None:
96+
# A text bitmap is created with max width/height by default : text with solid background
97+
text_image = Image.new('RGB', (DISPLAY_WIDTH, DISPLAY_HEIGHT), background_color)
98+
else:
99+
# The text bitmap is created from provided background image : text with transparent background
100+
text_image = Image.open(background_image)
92101

93102
# Draw text with specified color & font
94103
font = ImageFont.truetype("./res/fonts/" + font, font_size)
95104
d = ImageDraw.Draw(text_image)
96-
d.text((0, 0), text, font=font, fill=font_color)
105+
d.text((x, y), text, font=font, fill=font_color)
97106

98-
# Crop text bitmap to the size of the text
107+
# Crop text bitmap to keep only the text
99108
text_width, text_height = d.textsize(text, font=font)
100-
text_image = text_image.crop((0, 0, text_width, text_height))
109+
text_image = text_image.crop((x, y, x + text_width, y + text_height))
101110

102111
DisplayPILImage(ser, text_image, x, y)
103112

@@ -117,11 +126,17 @@ def DisplayText(ser, text, x=0, y=0,
117126

118127
# Display sample text
119128
DisplayText(lcd_comm, "Basic text", 50, 100)
120-
121-
DisplayText(lcd_comm, "Custom text", 5, 150,
122-
font="roboto/Roboto-BoldItalic.ttf",
123-
font_size=40,
129+
130+
DisplayText(lcd_comm, "Custom italic text", 5, 150,
131+
font="roboto/Roboto-Italic.ttf",
132+
font_size=30,
124133
font_color=(0, 0, 255),
125134
background_color=(255, 255, 0))
126135

136+
DisplayText(lcd_comm, "Transparent bold text", 5, 300,
137+
font="roboto/Roboto-Bold.ttf",
138+
font_size=30,
139+
font_color=(255, 255, 255),
140+
background_image="res/example.png")
141+
127142
lcd_comm.close()

0 commit comments

Comments
 (0)