-
Notifications
You must be signed in to change notification settings - Fork 0
/
initials_avatar_generator.py
56 lines (40 loc) · 1.77 KB
/
initials_avatar_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# ! DISCLAIMER !
# Not my idea originally, based on youtube video by The Codeholic & uses code generated by OpenAI's ChatGPT.
# Link to original video: https://www.youtube.com/watch?v=gviBX43JnDA
# Imports
# Note! Uses pil library: https://pillow.readthedocs.io/en/stable/installation.html
from PIL import Image, ImageDraw, ImageFont
import random
def generate_image_from_name():
# prompt user to enter first name and last name
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
# set image size and random background color
size = (256, 256)
bg_color = tuple(random.randint(0, 255) for _ in range(3))
text_color = tuple(map(lambda i, j: i - j, (255, 255, 255), bg_color))
# create image and drawing object
image = Image.new("RGB", size, bg_color)
draw = ImageDraw.Draw(image)
# set font and text size
font = ImageFont.truetype("arial.ttf", 72)
# get initials from first and last name
initials = f"{first_name[0]}{last_name[0]}"
# get text size and position
text_size = draw.textsize(initials, font=font)
text_x = (size[0] - text_size[0]) / 2
text_y = (size[1] - text_size[1]) / 2
# draw initials on image
draw.text((text_x, text_y), initials, font=font, fill=text_color)
# create a circular mask
mask = Image.new('L', (256, 256), 0)
draw_thumb = ImageDraw.Draw(mask)
draw_thumb.ellipse((0, 0, 256, 256), fill=255)
# apply the mask to the image
image.putalpha(mask)
# save image with initials as filename
filename = f"images/{first_name}_{last_name}_avatar.png"
image.save(filename)
print(f"Avatar image saved as {filename}")
if __name__ == "__main__":
generate_image_from_name()