Skip to content

Commit 199df4a

Browse files
authored
Merge pull request #334 from mathoudebine/fix/289-high-rate-of-accessing-a-background-file
2 parents 2ab027b + adf6323 commit 199df4a

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

library/lcd/lcd_comm.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

19+
import copy
1920
import os
2021
import queue
2122
import sys
@@ -62,6 +63,13 @@ def __init__(self, com_port: str = "AUTO", display_width: int = 320, display_hei
6263
# mixed with other requests in-between
6364
self.update_queue_mutex = threading.Lock()
6465

66+
# Create a cache to store opened images, to avoid opening and loading from the filesystem every time
67+
self.image_cache = {} # { key=path, value=PIL.Image }
68+
69+
# Create a cache to store opened fonts, to avoid opening and loading from the filesystem every time
70+
self.font_cache = {} # { key=(font, size), value=PIL.ImageFont }
71+
72+
6573
def get_width(self) -> int:
6674
if self.orientation == Orientation.PORTRAIT or self.orientation == Orientation.REVERSE_PORTRAIT:
6775
return self.display_width
@@ -194,7 +202,7 @@ def DisplayPILImage(
194202
pass
195203

196204
def DisplayBitmap(self, bitmap_path: str, x: int = 0, y: int = 0, width: int = 0, height: int = 0):
197-
image = Image.open(bitmap_path)
205+
image = self.open_image(bitmap_path)
198206
self.DisplayPILImage(image, x, y, width, height)
199207

200208
def DisplayText(
@@ -235,10 +243,12 @@ def DisplayText(
235243
)
236244
else:
237245
# The text bitmap is created from provided background image : text with transparent background
238-
text_image = Image.open(background_image)
246+
text_image = self.open_image(background_image)
239247

240248
# Get text bounding box
241-
font = ImageFont.truetype("./res/fonts/" + font, font_size)
249+
if (font, font_size) not in self.font_cache:
250+
self.font_cache[(font, font_size)] = ImageFont.truetype("./res/fonts/" + font, font_size)
251+
font = self.font_cache[(font, font_size)]
242252
d = ImageDraw.Draw(text_image)
243253
left, top, right, bottom = d.textbbox((x, y), text, font=font, align=align, anchor=anchor)
244254

@@ -294,7 +304,7 @@ def DisplayProgressBar(self, x: int, y: int, width: int, height: int, min_value:
294304
bar_image = Image.new('RGB', (width, height), background_color)
295305
else:
296306
# A bitmap is created from provided background image
297-
bar_image = Image.open(background_image)
307+
bar_image = self.open_image(background_image)
298308

299309
# Crop bitmap to keep only the progress bar background
300310
bar_image = bar_image.crop(box=(x, y, x + width, y + height))
@@ -372,7 +382,7 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
372382
bar_image = Image.new('RGB', (diameter, diameter), background_color)
373383
else:
374384
# A bitmap is created from provided background image
375-
bar_image = Image.open(background_image)
385+
bar_image = self.open_image(background_image)
376386

377387
# Crop bitmap to keep only the progress bar background
378388
bar_image = bar_image.crop(box=bbox)
@@ -464,3 +474,10 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
464474
font=font, fill=font_color)
465475

466476
self.DisplayPILImage(bar_image, xc - radius, yc - radius)
477+
478+
# Load image from the filesystem, or get from the cache if it has already been loaded previously
479+
def open_image(self, bitmap_path: str) -> Image:
480+
if bitmap_path not in self.image_cache:
481+
logger.debug("Bitmap " + bitmap_path + " is now loaded in the cache")
482+
self.image_cache[bitmap_path] = Image.open(bitmap_path)
483+
return copy.copy(self.image_cache[bitmap_path])

0 commit comments

Comments
 (0)