1
- from octologo .wizard import TextQuestion , SelectQuestion
2
- from textual .validation import Number
3
- from octologo .utils import font_list , color_scheme_names , FONTS_DIR , color_schemes , os , get_font_height , get_text_size
4
- from PIL import Image , ImageDraw , ImageFont , ImageColor
5
1
import sys
6
2
3
+ from octologo .utils import (
4
+ FONTS_DIR ,
5
+ color_scheme_names ,
6
+ color_schemes ,
7
+ font_list ,
8
+ get_font_height ,
9
+ get_text_size ,
10
+ os ,
11
+ )
12
+ from octologo .wizard import SelectQuestion , TextQuestion
13
+ from PIL import Image , ImageColor , ImageDraw , ImageFont
14
+ from PIL .Image import Image as ImageClass
15
+ from PIL .ImageFont import FreeTypeFont
16
+ from textual .validation import Number
17
+
7
18
sys .path .append (".." )
8
19
9
20
10
21
questions = [
11
- SelectQuestion ("font" , "Select a font" , [(font , font ) for font in font_list ], "Iosevka-Nerd-Font-Complete.ttf" ),
22
+ SelectQuestion (
23
+ "font" ,
24
+ "Select a font" ,
25
+ [(font , font ) for font in font_list ],
26
+ "Iosevka-Nerd-Font-Complete.ttf" ,
27
+ ),
12
28
SelectQuestion ("color" , "Select a color scheme" , color_scheme_names , "adi1090x" ),
13
- TextQuestion ("underline_count" , "Lettrs to undrline" , [Number (minimum = 0 )], "1" , "1" ),
29
+ TextQuestion (
30
+ "underline_count" , "Lettrs to undrline" , [Number (minimum = 0 )], "1" , "1"
31
+ ),
14
32
TextQuestion ("padding_x" , "Padding x (px)" , [Number ()], "200" , "200" ),
15
33
TextQuestion ("padding_y" , "Padding y (px)" , [Number ()], "20" , "20" ),
16
34
TextQuestion ("gap" , "Gap between text and bar (px)" , [Number ()], "20" , "20" ),
17
35
TextQuestion ("bar_size" , "Bar weight (px)" , [Number ()], "20" , "20" ),
18
- TextQuestion ("additionnal_bar_width" , "Additionnal bar width (px)" , [Number ()], "20" , "20" ),
36
+ TextQuestion (
37
+ "additionnal_bar_width" , "Additionnal bar width (px)" , [Number ()], "20" , "20"
38
+ ),
19
39
]
20
40
21
41
active = False
22
42
23
43
24
- def get_image (answers ) :
44
+ def get_image (answers : dict ) -> ImageClass :
25
45
# Load the selected font
26
46
font_size = 500
27
- font = ImageFont .truetype (os .path .join (FONTS_DIR , answers ["font" ]), font_size )
47
+ font : FreeTypeFont = ImageFont .truetype (os .path .join (FONTS_DIR , answers ["font" ]), font_size )
28
48
29
49
# Set the colors
30
- background = ImageColor .getrgb (color_schemes [answers [' color' ]]["background" ])
31
- text = ImageColor .getrgb (color_schemes [answers [' color' ]]["text" ])
32
- accent = ImageColor .getrgb (color_schemes [answers [' color' ]]["accent" ])
50
+ background = ImageColor .getrgb (color_schemes [answers [" color" ]]["background" ])
51
+ text = ImageColor .getrgb (color_schemes [answers [" color" ]]["text" ])
52
+ accent = ImageColor .getrgb (color_schemes [answers [" color" ]]["accent" ])
33
53
34
54
# Get the width and height of the texts
35
- text_width , text_height = get_text_size (answers [' name' ], font )
55
+ text_width , text_height = get_text_size (answers [" name" ], font )
36
56
font_height = get_font_height (font )
37
57
38
58
# Get the correct image width and height
39
- image_width = 2 * int (answers [' padding_x' ]) + text_width
40
- image_height = 2 * int (answers [' padding_y' ]) + font_height
59
+ image_width = 2 * int (answers [" padding_x" ]) + text_width
60
+ image_height = 2 * int (answers [" padding_y" ]) + font_height
41
61
42
62
# Create the image
43
63
image = Image .new ("RGB" , (image_width , image_height ), background )
@@ -46,39 +66,42 @@ def get_image(answers):
46
66
# Get the text anchor type and position on the image (where the text will be drawn)
47
67
# LM = Left/Middle
48
68
anchor_type = "lm"
49
- anchor_x = int (answers [' padding_x' ])
50
- anchor_y = image_height / 2 - (int (answers [' gap' ]) + int (answers [' bar_size' ])) / 2
69
+ anchor_x = int (answers [" padding_x" ])
70
+ anchor_y = image_height / 2 - (int (answers [" gap" ]) + int (answers [" bar_size" ])) / 2
51
71
52
72
anchor_pos = (anchor_x , anchor_y )
53
73
54
- if int (answers [' underline_count' ]) > 0 :
74
+ if int (answers [" underline_count" ]) > 0 :
55
75
# Get the bbox of the first n letter to underline
56
76
first_letters_bbox = draw .textbbox (
57
77
anchor_pos ,
58
- answers [' name' ][:int (answers [' underline_count' ])],
78
+ answers [" name" ][: int (answers [" underline_count" ])],
59
79
font = font ,
60
- anchor = anchor_type )
80
+ anchor = anchor_type ,
81
+ )
61
82
62
83
# Get the underline position
63
- underline_start_x = first_letters_bbox [0 ] - int (answers ['additionnal_bar_width' ])
64
- underline_start_y = first_letters_bbox [3 ] + int (answers ['gap' ])
84
+ underline_start_x = first_letters_bbox [0 ] - int (
85
+ answers ["additionnal_bar_width" ]
86
+ )
87
+ underline_start_y = first_letters_bbox [3 ] + int (answers ["gap" ])
65
88
66
- underline_end_x = int (answers [' additionnal_bar_width' ]) + first_letters_bbox [2 ]
89
+ underline_end_x = int (answers [" additionnal_bar_width" ]) + first_letters_bbox [2 ]
67
90
68
- underline_end_y = underline_start_y + int (answers [' bar_size' ])
91
+ underline_end_y = underline_start_y + int (answers [" bar_size" ])
69
92
70
93
underline_start = (underline_start_x , underline_start_y )
71
94
underline_end = (underline_end_x , underline_end_y )
72
95
73
96
underline_pos = [underline_start , underline_end ]
74
97
75
98
# Underline the first letter
76
- draw .rectangle (underline_pos , fill = accent , width = answers [' bar_size' ])
99
+ draw .rectangle (underline_pos , fill = accent , width = answers [" bar_size" ])
77
100
78
101
# Draw the text
79
102
draw .text (
80
103
anchor_pos ,
81
- answers [' name' ],
104
+ answers [" name" ],
82
105
font = font ,
83
106
fill = text ,
84
107
anchor = anchor_type ,
@@ -87,7 +110,7 @@ def get_image(answers):
87
110
# Redraw the first letter
88
111
draw .text (
89
112
anchor_pos ,
90
- answers [' name' ][0 ],
113
+ answers [" name" ][0 ],
91
114
font = font ,
92
115
fill = accent ,
93
116
anchor = anchor_type ,
0 commit comments