Skip to content

Commit

Permalink
Merge pull request #334 from mathoudebine/fix/289-high-rate-of-access…
Browse files Browse the repository at this point in the history
…ing-a-background-file
  • Loading branch information
mathoudebine authored Sep 23, 2023
2 parents 2ab027b + adf6323 commit 199df4a
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions library/lcd/lcd_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import copy
import os
import queue
import sys
Expand Down Expand Up @@ -62,6 +63,13 @@ def __init__(self, com_port: str = "AUTO", display_width: int = 320, display_hei
# mixed with other requests in-between
self.update_queue_mutex = threading.Lock()

# Create a cache to store opened images, to avoid opening and loading from the filesystem every time
self.image_cache = {} # { key=path, value=PIL.Image }

# Create a cache to store opened fonts, to avoid opening and loading from the filesystem every time
self.font_cache = {} # { key=(font, size), value=PIL.ImageFont }


def get_width(self) -> int:
if self.orientation == Orientation.PORTRAIT or self.orientation == Orientation.REVERSE_PORTRAIT:
return self.display_width
Expand Down Expand Up @@ -194,7 +202,7 @@ def DisplayPILImage(
pass

def DisplayBitmap(self, bitmap_path: str, x: int = 0, y: int = 0, width: int = 0, height: int = 0):
image = Image.open(bitmap_path)
image = self.open_image(bitmap_path)
self.DisplayPILImage(image, x, y, width, height)

def DisplayText(
Expand Down Expand Up @@ -235,10 +243,12 @@ def DisplayText(
)
else:
# The text bitmap is created from provided background image : text with transparent background
text_image = Image.open(background_image)
text_image = self.open_image(background_image)

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

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

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

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

self.DisplayPILImage(bar_image, xc - radius, yc - radius)

# Load image from the filesystem, or get from the cache if it has already been loaded previously
def open_image(self, bitmap_path: str) -> Image:
if bitmap_path not in self.image_cache:
logger.debug("Bitmap " + bitmap_path + " is now loaded in the cache")
self.image_cache[bitmap_path] = Image.open(bitmap_path)
return copy.copy(self.image_cache[bitmap_path])

0 comments on commit 199df4a

Please sign in to comment.