Skip to content
This repository was archived by the owner on Mar 23, 2025. It is now read-only.

Commit f69a9fd

Browse files
committed
add centered label class
1 parent bc8c43e commit f69a9fd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

labelprinterkit/label.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,36 @@ def render(self, width=None, height=None) -> Image:
8282
return img
8383

8484
# print("".join(f"{x:08b}".replace("0", " ") for x in bytes(i)))
85+
86+
class CenteredLabel(Label):
87+
def render(self, width=None, height=None) -> Image:
88+
"""render the Label.
89+
90+
Args:
91+
width: Width request
92+
height: Height request
93+
"""
94+
size = self.size
95+
img = Image.new("1", size, "white")
96+
97+
pos = [0, 0]
98+
99+
for line in self._rendered_items:
100+
line_width = sum(item.size[0] for item in line)
101+
# to center, offset by half of different between line width and total width
102+
pos[0] = (size[0] - line_width) // 2
103+
for item in line:
104+
box = (*pos, *_coord_add(item.size, pos))
105+
img.paste(item, box=box)
106+
pos[0] += item.size[0]
107+
108+
pos[1] += max(i.size[1] for i in line)
109+
110+
xdim, ydim = img.size
111+
print("presize", xdim, ydim, height)
112+
xdim = round((height / ydim) * xdim)
113+
114+
print("calcsize", xdim, ydim)
115+
img = img.resize((xdim, height))
116+
117+
return img

0 commit comments

Comments
 (0)