Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # smart_font_resize.py ZZZ note: actual font sizes seems too awkward
- from PIL import ImageFont
- import os
- text = "Hello Python World!"
- max_width = 600
- def get_system_fonts():
- font_dirs = [
- "/usr/share/fonts", # Linux
- "/Library/Fonts", # macOS
- "C:/Windows/Fonts", # Windows
- ]
- fonts = []
- for font_dir in font_dirs:
- if os.path.exists(font_dir):
- for root, _, files in os.walk(font_dir):
- for file in files:
- if file.endswith((".ttf", ".otf")):
- fonts.append(os.path.join(root, file))
- return fonts
- # system_fonts = get_system_fonts()
- system_fonts = '''
- C:/Windows/Fonts/arial.ttf
- C:/Windows/Fonts/bahnschrift.ttf
- C:/Windows/Fonts/calibri.ttf
- C:/Windows/Fonts/Candara.ttf
- C:/Windows/Fonts/comic.ttf
- C:/Windows/Fonts/consola.ttf
- C:/Windows/Fonts/corbel.ttf
- C:/Windows/Fonts/cour.ttf
- C:/Windows/Fonts/Deng.ttf
- C:/Windows/Fonts/ebrima.ttf
- C:/Windows/Fonts/Gabriola.ttf
- C:/Windows/Fonts/gadugi.ttf
- C:/Windows/Fonts/georgia.ttf
- C:/Windows/Fonts/himalaya.ttf
- C:/Windows/Fonts/holomdl2.ttf
- C:/Windows/Fonts/impact.ttf
- C:/Windows/Fonts/Inkfree.ttf
- C:/Windows/Fonts/LeelawUI.ttf
- C:/Windows/Fonts/lucon.ttf
- C:/Windows/Fonts/Nirmala.ttf
- '''.strip().splitlines()
- for font in system_fonts:
- print(font)
- print(f"Total fonts found: {len(system_fonts)}")
- font_adj = []
- for font_path in system_fonts:
- min_size = 1
- max_size = 1000
- adjustment_history = [font_path]
- while min_size <= max_size:
- mid_size = (min_size + max_size) // 2
- label_font = ImageFont.truetype(font_path, mid_size)
- text_width = label_font.getlength(text)
- adjustment = max_width - text_width
- adjustment_history.append(mid_size)
- if adjustment > 0:
- min_size = mid_size + 1
- else:
- max_size = mid_size - 1
- font_adj.append(adjustment_history)
- print(adjustment)
- for adj in font_adj:
- print(adj)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement