Skip to content

Commit

Permalink
(agrf) Manage resources/ directory with pkgutils as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyangyi committed Oct 13, 2024
1 parent 9a7abbd commit 18d24b3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion agrf/pkg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import io
import pkgutil
from PIL import Image
from PIL import Image, ImageFont


def load_third_party_image(path):
content = pkgutil.get_data("agrf", path)
f = io.BytesIO(content)
return Image.open(f)


def load_font(path, size):
content = pkgutil.get_data("agrf", path)
f = io.BytesIO(content)
return ImageFont.truetype(f, size)
5 changes: 5 additions & 0 deletions agrf/pkg/font_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import load_font


def test_font():
assert load_font("resources/AntaeusConsoleNumbers.otf", 12) is not None
File renamed without changes.
3 changes: 2 additions & 1 deletion agrf/sprites/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import grf
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from agrf.pkg import load_font

THIS_FILE = grf.PythonFile(__file__)

Expand All @@ -24,7 +25,7 @@ def get_data_layers(self, context):
alpha = Image.fromarray(alpha)
draw = ImageDraw.Draw(img)
drawa = ImageDraw.Draw(alpha)
font = ImageFont.truetype("resources/AntaeusConsoleNumbers.otf", w // 4)
font = load_font("resources/AntaeusConsoleNumbers.otf", w // 4)
message = str(self.number)
_, _, _w, _h = draw.textbbox((0, 0), message, font=font)
draw.text(((w - _w) // 2, w // 4), message, "blue", font=font)
Expand Down

0 comments on commit 18d24b3

Please sign in to comment.