|
| 1 | +from PIL import Image, ImageDraw, ImageFont |
| 2 | + |
| 3 | +import matplotlib.font_manager |
| 4 | + |
| 5 | +size = 256 |
| 6 | + |
| 7 | +# Create a new black image with a transparent background |
| 8 | +image_size = (size, size) |
| 9 | +background_color = (0, 0, 0, 255) |
| 10 | +image = Image.new("RGBA", image_size, background_color) |
| 11 | + |
| 12 | +# Print available fonts |
| 13 | + |
| 14 | +# Load a monospace font |
| 15 | +# Could be: FiraCode-Retina |
| 16 | +font_size = int(size / 2) |
| 17 | +try: |
| 18 | + font = ImageFont.truetype("FiraCode-Retina.ttf", font_size) |
| 19 | +except OSError: |
| 20 | + print("Available fonts:", [font.split("/")[-1] for font in matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')]) |
| 21 | + raise |
| 22 | + |
| 23 | +# Draw the string "$ su" on the image |
| 24 | +def draw_text_old(): |
| 25 | + text = "$ su" |
| 26 | + text_color = (255, 255, 255, 255) |
| 27 | + draw = ImageDraw.Draw(image) |
| 28 | + text_width, text_height = draw.textsize(text, font=font) |
| 29 | + text_position = ((image_size[0] - text_width) // 2, (image_size[1] - text_height) // 2) |
| 30 | + draw.text(text_position, text, font=font, fill=text_color) |
| 31 | + |
| 32 | +# Draw the string "$" and "su" on the image with adjusted spacing |
| 33 | +def draw_text(): |
| 34 | + draw = ImageDraw.Draw(image) |
| 35 | + dollar_sign = "$" |
| 36 | + text = "su" |
| 37 | + text_color = (255, 255, 255, 255) |
| 38 | + |
| 39 | + left, top, right, bottom = draw.textbbox((0, 0), dollar_sign, font=font) |
| 40 | + dollar_sign_width = right - left |
| 41 | + dollar_sign_height = bottom - top |
| 42 | + left, top, right, bottom = draw.textbbox((0, 0), text, font=font) |
| 43 | + text_width = right - left |
| 44 | + text_height = bottom - top |
| 45 | + print("Dollar:", (dollar_sign_width, dollar_sign_height)) |
| 46 | + print("Text su:", (text_width, text_height)) |
| 47 | + |
| 48 | + spacing = int(font_size / 8) # Adjust this value to control the spacing between the "$" and "su" |
| 49 | + print("Spacing:", spacing) |
| 50 | + total_width = dollar_sign_width + spacing + text_width |
| 51 | + print("Total width:", total_width) |
| 52 | + |
| 53 | + |
| 54 | + dollar_sign_position = ((image_size[0] - total_width) // 2, |
| 55 | + (image_size[1] - dollar_sign_height) // 2) |
| 56 | + text_position = (dollar_sign_position[0] + dollar_sign_width + spacing, |
| 57 | + (image_size[1] - dollar_sign_height) // 2) |
| 58 | + |
| 59 | + draw.text(dollar_sign_position, dollar_sign, font=font, fill=text_color) |
| 60 | + draw.text(text_position, text, font=font, fill=text_color) |
| 61 | + |
| 62 | +# Actually draw |
| 63 | +draw_text() |
| 64 | + |
| 65 | +# Save the image as a favicon |
| 66 | +image.save("media/favicon.ico", "ICO") |
| 67 | +print("Done!") |
0 commit comments