Skip to content

Commit 7055673

Browse files
committed
Add --font-scale, --h-padding, --v-shift and generalize --end-margin
Fixes #2
1 parent 2715496 commit 7055673

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ In addition, all options included in *labelmaker.py* are available, with several
3434
```
3535
usage: printlabel.py [-h] [-u] [-l] [-s] [-c] [-i FILE_NAME] [-M FILE_NAME] [-R FLOAT] [-X DOTS]
3636
[-Y DOTS] [-S FILE_NAME] [-n] [-F] [-a] [-m DOTS] [-r] [-C]
37-
[--fill-color FILL] [--stroke-fill STROKE_FILL]
38-
[--stroke-width STROKE_WIDTH] [--text-size MILLIMETERS]
39-
[--white-level NUMBER] [--threshold NUMBER]
40-
COM_PORT FONT_NAME TEXT_TO_PRINT [TEXT_TO_PRINT ...]
37+
[--fill-color FILL] [--stroke-fill STROKE_FILL] [--stroke-width STROKE_WIDTH]
38+
[--text-size MILLIMETERS] [--font-scale NUMBER] [--h-padding DOTS]
39+
[--v-shift DOTS] [--white-level NUMBER] [--threshold NUMBER]
40+
COM_PORT [FONT_NAME] [TEXT_TO_PRINT ...]
4141
4242
positional arguments:
4343
COM_PORT Printer COM port.
@@ -78,15 +78,20 @@ optional arguments:
7878
Width of the text stroke (e.g., 1 or 2).
7979
--text-size MILLIMETERS
8080
Horizontally stretch the text to fit the specified size.
81-
--white-level NUMBER Minimum pixel value to consider it "white" when cropping the image. Set
82-
it to a value close to 255. (Default: 240)
81+
--font-scale NUMBER Scale font size by specified percentage (default: 100%)
82+
--h-padding DOTS Define custom left and right horizontal padding in pixels (default: 5
83+
pixels left and 5 pixels right)
84+
--v-shift DOTS Define relative vertical traslation in pixels (default is to vertically
85+
center the font)
86+
--white-level NUMBER Minimum pixel value to consider it "white" when cropping the image. Set it
87+
to a value close to 255. (Default: 240)
8388
--threshold NUMBER Custom thresholding when converting the image to binary, to manually
8489
decide which pixel values become black or white (Default: 75)
8590
```
8691

8792
Options `-sln` are useful to simulate the print, showing the created image and adding a ruler in inches and centimeters (magenta), with horizontal lines to mark the drawing area (dotted red) and the tape borders (cyan).
8893

89-
Before generating the text (`TEXT_TO_PRINT`), the tool allows concatenating images with the `-M` option; it can be used more times for multiple images (transparent images are also accepted). The final image can also be saved with the `-S` option and then reused by running again the tool with the `-M` option; when also setting `TEXT_TO_PRINT` to a null string (`""`), the reused image will remain unchanged. Merged images are automatically resized to fit the printable area, removing white borders without modifying the proportion. Resize and traslation of merged images can also be manually controlled with `-R` (floating point number), `-X`, `-Y`. The `--text-size` option horizontally stretches or squeezes the text so that it fits the specified size in millimeters; the size parameter includes `--end-margin` and default left and right paddings, but does not include the size of merged images if used, which have a fixed length that has to be kept proportioned.
94+
Before generating the text (`TEXT_TO_PRINT`), the tool allows concatenating images with the `-M` option; it can be used more times for multiple images (transparent images are also accepted). The final image can also be saved with the `-S` option and then reused by running again the tool with the `-M` option; when also setting `TEXT_TO_PRINT` to a null string (`""`), the reused image will remain unchanged. Merged images are automatically resized to fit the printable area, removing white borders without modifying the proportion. Resize and traslation of merged images can also be manually controlled with `-R` (floating point number), `-X`, `-Y`. The `--text-size` option horizontally stretches or squeezes the text so that it fits the specified size in millimeters; the size parameter includes `--end-margin` and default left and right paddings, but does not include the size of merged images if used, which have a fixed length that has to be kept proportioned. The `font-scale` allows specifying a percentage to scale the font size, maintaining the aspect ratio; font sizes > 100 are accepted even if potentially causing overflow. `--h-padding` and `--v-shift` allow horizontally and vertically traslating the text (using `--h-padding` with `--end-margin` enables separately cointrolling left and right margins; specifically `--h-padding` uses the same value for the left and right parts, while `--end-margin` will be a relative value applied to the right `--h-padding`).
9095

9196
`-i` runs the legacy process of *labelmaker.py* and disables image processing.
9297

printlabel.py

+48-5
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ def set_args():
145145
type=int,
146146
default=None,
147147
)
148+
p.add_argument(
149+
'--font-scale',
150+
type=float,
151+
default=None,
152+
metavar='NUMBER',
153+
help='Scale font size by specified percentage (default: 100%%)'
154+
)
155+
p.add_argument(
156+
'--h-padding',
157+
type=int,
158+
default=5,
159+
metavar='DOTS',
160+
help='Define custom left and right horizontal padding in pixels'
161+
' (default: 5 pixels left and 5 pixels right)'
162+
)
163+
p.add_argument(
164+
'--v-shift',
165+
type=int,
166+
default=0,
167+
metavar='DOTS',
168+
help='Define relative vertical traslation in pixels'
169+
' (default is to vertically center the font)'
170+
)
148171
p.add_argument(
149172
'--white-level',
150173
help='Minimum pixel value to consider it "white" when'
@@ -242,7 +265,6 @@ def main():
242265
height_of_the_printable_area = 64 # px: number of vertical pixels of the PT-P300BT printer (9 mm)
243266
height_of_the_tape = 86 # 64 px / 9 mm * 12 mm (the borders over the printable area will not be printed)
244267
height_of_the_image = 88 # px (can be any value >= height_of_the_tape, but height_of_the_tape + 2 border lines is good)
245-
h_padding = 5 # horizontal padding (left and right)
246268

247269
# Compute max TT font size to remain within height_of_the_printable_area
248270
font_size = 0
@@ -273,16 +295,37 @@ def main():
273295
f' instead of {height_of_the_printable_area}.')
274296
break
275297

298+
y_position = print_border
299+
if args.font_scale:
300+
scaled_font_size = int(
301+
round(font_size * (args.font_scale / 100.0))
302+
)
303+
try:
304+
font = ImageFont.truetype(
305+
args.fontname, scaled_font_size, encoding='utf-8'
306+
)
307+
except Exception as e:
308+
p.error(f'Cannot load font "{args.fontname}" - {e}')
309+
font_width, font_height = font.getbbox(text, anchor="lt")[2:]
310+
311+
# Vertically center text
312+
y_position = print_border + (
313+
height_of_the_printable_area - font_height
314+
) // 2
315+
276316
# Create a drawing context for the image
277317
image = Image.new(
278318
"RGB",
279-
(font_width + h_padding * 2 + 1, height_of_the_image),
319+
(
320+
font_width + args.h_padding * 2 + 1 + args.end_margin,
321+
height_of_the_image
322+
),
280323
"white"
281324
)
282325
draw = ImageDraw.Draw(image)
283326
try:
284327
draw.text(
285-
(h_padding, print_border), text,
328+
(args.h_padding, y_position + args.v_shift), text,
286329
font=font,
287330
fill=args.fill,
288331
anchor="lt",
@@ -294,7 +337,7 @@ def main():
294337
if args.text_size:
295338
text_size = (
296339
int(args.text_size / 0.149)
297-
- h_padding
340+
- args.h_padding
298341
- args.end_margin
299342
) # mm to dot
300343
_, _, text_width, text_height = draw.textbbox(
@@ -305,7 +348,7 @@ def main():
305348
)
306349
scale_factor = text_width / text_size
307350
image = image.transform(
308-
(text_size + args.end_margin, height_of_the_image),
351+
(text_size + args.end_margin + args.h_padding, height_of_the_image),
309352
Image.Transform.AFFINE,
310353
(scale_factor, 0, 0, 0, 1, 0),
311354
)

0 commit comments

Comments
 (0)