Skip to content

Instantly share code, notes, and snippets.

@Chizaruu
Last active April 24, 2024 10:43
Show Gist options
  • Select an option

  • Save Chizaruu/567981b34745f7b08cbaac07bdab5240 to your computer and use it in GitHub Desktop.

Select an option

Save Chizaruu/567981b34745f7b08cbaac07bdab5240 to your computer and use it in GitHub Desktop.
LogoThumbnailMaker
# Install the required dependencies by running the following command in the terminal:
# pip install pillow
import os
from PIL import Image, ImageDraw, ImageFont
# Define the background color
bg_color = (54, 57, 63)
# Define the image size
width = 1280
height = 720
# Define the logo size and spacing
logo_size = 110
logo_spacing = 125
# Choose the overlay opacity option (0-255)
# 0 means fully transparent, 255 means fully opaque
overlay_opacity = 127
# Define the title text and font
title_text = "ROGUELIKE TUTORIAL Part 1"
subtitle_text = "Instantiating the ‘@’ symbol and moving it around"
# Maximum width of the title text (80% of the image width)
max_title_width = width
font_size = 145
subtitle_font_size = 100
regular_font = ImageFont.truetype(
"System/Library/Fonts/impact.ttf", font_size)
bold_font = ImageFont.truetype(
"System/Library/Fonts/GILLUBCD.TTF", font_size) # Bold font
subtitle_font = ImageFont.truetype(
"System/Library/Fonts/impact.ttf", subtitle_font_size)
# Minimum number of lines for the title text
min_lines = 1 # Set to 1 for no minimum
# Choose the font option
# Options: 'regular', 'bold'
font_option = 'regular'
# Text outline settings
text_outline_width = 1 # Adjust the outline width as desired
text_outline_color = (0, 0, 0, 255) # Black outline color (RGBA)
# Choose the alignment option
# Options: 'left', 'right', 'center', 'justified'
alignment = 'left'
# Specify the folder containing the logo images
logo_folder = 'logos'
# Get the list of logo image files from the folder
logo_files = [file for file in os.listdir(
logo_folder) if file.endswith('.png') or file.endswith('.jpg')]
# Load and resize the logo images
logos = []
for file in logo_files:
logo = Image.open(os.path.join(logo_folder, file))
logo.thumbnail((logo_size, logo_size))
logos.append(logo)
# Create a new image with the Discord background color
img = Image.new('RGBA', (width, height), bg_color)
# Choose the direction of the logo pattern
# Options: 'horizontal', 'vertical', 'diagonal_left', 'diagonal_right'
direction = 'horizontal'
# Draw the logos in a repeating pattern
logo_index = 0
if direction == 'horizontal':
for y in range(0, height, logo_spacing):
for x in range(0, width, logo_spacing):
if x < width:
bg_image = Image.new('RGBA', (logo_size, logo_size), bg_color)
bg_image.paste(logos[logo_index], (0, 0),
mask=logos[logo_index])
img.paste(bg_image, (x, y))
logo_index = (logo_index + 1) % len(logos)
elif direction == 'vertical':
for x in range(0, width, logo_spacing):
for y in range(0, height, logo_spacing):
if y < height:
bg_image = Image.new('RGBA', (logo_size, logo_size), bg_color)
bg_image.paste(logos[logo_index], (0, 0),
mask=logos[logo_index])
img.paste(bg_image, (x, y))
logo_index = (logo_index + 1) % len(logos)
elif direction == 'diagonal_left':
for y in range(0, height + logo_spacing, logo_spacing - 5):
for x in range(0, width + logo_spacing, logo_spacing - 15):
if x < width and y < height:
bg_image = Image.new('RGBA', (logo_size, logo_size), bg_color)
bg_image.paste(logos[logo_index], (0, 0),
mask=logos[logo_index])
img.paste(bg_image, (x, y))
logo_index = (logo_index + 1) % len(logos)
elif direction == 'diagonal_right':
for y in range(0, height + logo_spacing, logo_spacing + 5):
for x in range(0, width + logo_spacing, logo_spacing + 15):
if x < width and y < height:
bg_image = Image.new('RGBA', (logo_size, logo_size), bg_color)
bg_image.paste(logos[logo_index], (0, 0),
mask=logos[logo_index])
img.paste(bg_image, (x, y))
logo_index = (logo_index + 1) % len(logos)
# Create a black overlay
overlay = Image.new('RGBA', (width, height), (0, 0, 0, 0))
overlay_draw = ImageDraw.Draw(overlay)
# Draw the overlay rectangle with the chosen opacity
overlay_draw.rectangle((0, 0, width, height),
fill=(0, 0, 0, overlay_opacity))
# Wrap the title text if it exceeds the maximum width
title_lines = []
current_line = ""
if font_option == 'regular':
font = regular_font
else:
font = bold_font
words = title_text.split()
for word in words:
# check if the word is the last word
if word == words[-1] and len(title_lines) + 1 < min_lines:
continue
if overlay_draw.textbbox((0, 0), current_line + " " + word, font)[2] <= max_title_width:
current_line += " " + word
else:
title_lines.append(current_line.strip())
current_line = word
if current_line:
title_lines.append(current_line.strip())
# Ensure the minimum number of lines
while len(title_lines) < min_lines:
if not words:
title_lines.append("")
else:
next_word = words.pop(-1)
title_lines.append(next_word)
# Wrap the subtitle text if it exceeds the maximum width
subtitle_lines = []
current_line = ""
for word in subtitle_text.split():
if overlay_draw.textbbox((0, 0), current_line + " " + word, subtitle_font)[2] <= max_title_width:
current_line += " " + word
else:
subtitle_lines.append(current_line.strip())
current_line = word
if current_line:
subtitle_lines.append(current_line.strip())
# Calculate the position to align the title and subtitle
title_height = sum(overlay_draw.textbbox(
(0, 0), line, font)[3] for line in title_lines)
subtitle_height = sum(overlay_draw.textbbox(
(0, 0), line, subtitle_font)[3] for line in subtitle_lines)
title_spacing = 10 # Spacing between title lines
subtitle_spacing = 5 # Spacing between subtitle lines
total_title_height = title_height + (len(title_lines) - 1) * title_spacing
total_subtitle_height = subtitle_height + \
(len(subtitle_lines) - 1) * subtitle_spacing
total_text_height = total_title_height + total_subtitle_height + \
30 # Spacing between title and subtitle
title_y = (height - total_text_height) // 2
subtitle_y = title_y + total_title_height + 30
# Draw the title text on the overlay
for line in title_lines:
line_bbox = overlay_draw.textbbox((0, 0), line, font)
line_width = line_bbox[2] - line_bbox[0]
line_height = line_bbox[3] - line_bbox[1]
# Determine the initial x-position based on the alignment option
if alignment == 'left':
title_x = 20 # Adjust this value to change the left margin
elif alignment == 'right':
# Adjust the value 20 to change the right margin
title_x = width - max_title_width - 20
elif alignment == 'center':
title_x = (width - line_width) // 2
elif alignment == 'justified':
total_width = max_title_width
words = line.split()
word_widths = [overlay_draw.textbbox((0, 0), word, font)[
2] for word in words]
word_spaces = len(words) - 1
if word_spaces > 0:
space_width = (total_width - sum(word_widths)) / word_spaces
else:
space_width = 0
current_x = 0
for i, word in enumerate(words):
for offset_x in range(-text_outline_width, text_outline_width + 1):
for offset_y in range(-text_outline_width, text_outline_width + 1):
if offset_x == 0 and offset_y == 0:
continue
outline_position = (
current_x + offset_x, title_y + offset_y)
overlay_draw.text(outline_position, word,
font=font, fill=text_outline_color)
overlay_draw.text((current_x, title_y), word,
font=font, fill=(255, 255, 255, 255))
current_x += word_widths[i] + space_width
continue # Skip the remaining code for the 'justified' alignment
if alignment in ('left', 'right', 'center'):
for offset_x in range(-text_outline_width, text_outline_width + 1):
for offset_y in range(-text_outline_width, text_outline_width + 1):
if offset_x == 0 and offset_y == 0:
continue
outline_position = (title_x + offset_x, title_y + offset_y)
overlay_draw.text(outline_position, line,
font=font, fill=text_outline_color)
overlay_draw.text((title_x, title_y), line,
font=font, fill=(255, 255, 255, 255))
title_y += line_height + title_spacing
# Draw the subtitle text on the overlay
for line in subtitle_lines:
line_bbox = overlay_draw.textbbox((0, 0), line, subtitle_font)
line_width = line_bbox[2] - line_bbox[0]
line_height = line_bbox[3] - line_bbox[1]
if alignment == 'left':
subtitle_x = 20 # Adjust this value to change the left margin
elif alignment == 'right':
# Adjust the value 20 to change the right margin
subtitle_x = width - max_title_width - 20
elif alignment == 'center':
subtitle_x = (width - line_width) // 2
elif alignment == 'justified':
total_width = max_title_width
words = line.split()
word_widths = [overlay_draw.textbbox((0, 0), word, subtitle_font)[
2] for word in words]
word_spaces = len(words) - 1
if word_spaces > 0:
space_width = (total_width - sum(word_widths)) / word_spaces
else:
space_width = 0
current_x = 0
for i, word in enumerate(words):
overlay_draw.text((current_x, subtitle_y), word,
font=subtitle_font, fill=(255, 255, 255, 255))
current_x += word_widths[i] + space_width
continue # Skip the remaining code for the 'justified' alignment
if alignment in ('left', 'right', 'center'):
overlay_draw.text((subtitle_x, subtitle_y), line,
font=subtitle_font, fill=(255, 255, 255, 255))
subtitle_y += line_height + subtitle_spacing
# Blend the overlay with the main image
img = Image.alpha_composite(img, overlay)
# Blend the overlay with the main image
img = Image.alpha_composite(img, overlay)
# Save the image with the title name
img.save(title_text + ".png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment