Last active
June 12, 2024 22:39
-
-
Save vgmoose/e700d90c77ce23113c29c4ed7f5e111a to your computer and use it in GitHub Desktop.
Generate a "get it on Homebrew App Store" badge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw, ImageFont, ImageColor | |
class Args: | |
def __init__(self): | |
self.text = "Homebrew App Store" | |
self.subtext = "Get it on the" | |
self.bg_color = "#000000" | |
self.border_color = "#A6A6A6" | |
self.output = "hbasbadge.png" | |
self.icon_path = "icon-11.png" | |
args = Args() | |
# Define the font and size | |
text_height = 36 | |
text_height *= 2 | |
subfont = ImageFont.truetype("Arial", text_height / 1.6) | |
font = ImageFont.truetype("OpenSans-Regular.ttf", text_height) | |
# Create a dummy image to calculate the text size | |
dummy_img = Image.new('RGBA', (1, 1)) | |
dummy_draw = ImageDraw.Draw(dummy_img) | |
# Get the width and height of the text | |
text_width = dummy_draw.textlength(args.text, font) | |
# Calculate the size of the actual image | |
img_width = (140 + text_width + 80) # Add some padding and upscale | |
img_height = (80 + text_height + 40) # Add some padding and upscale | |
# Create the actual image | |
img = Image.new('RGBA', (int(img_width+2), int(img_height)), (255, 255, 255, 0)) | |
draw = ImageDraw.Draw(img) | |
# Draw a rectangle with rounded corners | |
radius = 20 | |
bgColor = ImageColor.getrgb(args.bg_color) | |
darkerBGColor = ImageColor.getrgb(args.border_color) | |
draw.rounded_rectangle([(0, 0), (img_width, img_height)], fill=bgColor, outline=darkerBGColor, width=5, radius=radius) | |
# Calculate position for the text to be in the center | |
x = 60 + (img.width - text_width) / 2 # Upscale the text width | |
y = 10 + (img.height - text_height) / 2 # Upscale the text height | |
# Add text | |
draw.text((x + 15, y - 35), args.subtext, fill="white", font=subfont) | |
draw.text((x + 10, y), args.text, fill="white", font=font) | |
# draw the icon | |
icon = Image.open(args.icon_path) | |
icon = icon.resize((120, 120), Image.LANCZOS) | |
img.paste(icon, (35, 35), icon) | |
# Save the image | |
img.save(args.output, dpi=(144, 144)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment