Skip to content

Commit

Permalink
Merge pull request #2 from littlebitstudios/dev-1.2
Browse files Browse the repository at this point in the history
Version 1.2
  • Loading branch information
jaherron authored Jul 29, 2024
2 parents b2a7482 + a533887 commit 33ef67f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 22 deletions.
115 changes: 94 additions & 21 deletions ENSCardMakerWeb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from PIL import Image, ImageDraw, ImageFont, ImageSequence
import textwrap
import io
import pydenticon
from io import BytesIO

app = Flask(__name__)

Expand All @@ -19,41 +21,104 @@ def generate_card(user):
profilejson = profilerequest.content.decode("utf-8")
profile = json.loads(profilejson)

avatar = None
identicon_used = False
fallback = False
if 'avatar' in profile:
avatar_response = requests.get(profile['avatar'], stream=True)
avatar = Image.open(avatar_response.raw)

if avatar.format == 'GIF':
avatar = next(ImageSequence.Iterator(avatar)) # Get the first frame of the GIF

avatar_size = (240, 240) # Specify the desired size of the avatar image
avatar = avatar.resize(avatar_size) # Resize the avatar image

try:
avatar_response = requests.get(profile['avatar'], stream=True)
avatar = Image.open(avatar_response.raw)

if avatar.format == 'GIF':
avatar = next(ImageSequence.Iterator(avatar)) # Get the first frame of the GIF

avatar_size = (240, 240) # Specify the desired size of the avatar image
avatar = avatar.resize(avatar_size) # Resize the avatar image

# Ensure the image is in RGB mode
if avatar.mode != 'RGB':
avatar = avatar.convert('RGB')

# Get the dominant color of the avatar
dominant_color = avatar.getpixel((0, 0))

# Darken the dominant color
darkened_color = tuple(int(c * 0.5) for c in dominant_color)

# Use the darkened color as the background color
img = Image.new('RGB', (740, 290), color=darkened_color)

except Exception as e:
identicon_used = True
fallback = True

# Generate the identicon image
identicon = pydenticon.Generator(5, 5).generate(profile['address'], 240, 240)

# Convert the identicon image to PIL Image format
avatar = Image.open(BytesIO(identicon))

# Ensure the image is in RGB mode
if avatar.mode != 'RGB':
avatar = avatar.convert('RGB')

# Get the dominant color of the avatar
dominant_color = avatar.getpixel((0, 0))

# Darken the dominant color
darkened_color = tuple(int(c * 0.5) for c in dominant_color)

# Use the darkened color as the background color
img = Image.new('RGB', (740, 290), color=darkened_color)
else:
identicon_used = True

# Generate the identicon image
identicon = pydenticon.Generator(5, 5).generate(profile['address'], 240, 240)

# Convert the identicon image to PIL Image format
avatar = Image.fromarray(identicon)

# Ensure the image is in RGB mode
if avatar.mode != 'RGB':
avatar = avatar.convert('RGB')

# Get the dominant color of the avatar
dominant_color = avatar.getpixel((0, 0))

# Darken the dominant color
darkened_color = tuple(int(c * 0.5) for c in dominant_color)

# Use the darkened color as the background color
img = Image.new('RGB', (740, 290), color=darkened_color)
else:
img = Image.new('RGB', (740, 290), color=(0, 0, 0))

draw = ImageDraw.Draw(img)
if 'avatar' in profile:
avatar_response = requests.get(profile['avatar'], stream=True)
avatar = Image.open(avatar_response.raw)
if avatar.format == 'GIF':
avatar = next(ImageSequence.Iterator(avatar)) # Get the first frame of the GIF

if avatar:
avatar_size = (240, 240) # Specify the desired size of the avatar image
avatar = avatar.resize(avatar_size) # Resize the avatar image
img.paste(avatar, (20, 20))
else:
draw.rectangle([20, 20, 260, 260], fill=(255, 255, 255))
if profile['records']['avatar'].startswith("eip155:1/") and not identicon_used:
# Draw a small box in the corner of the avatar
box_position = (30, 30, 80, 60) # Define the position of the box
draw.rectangle(box_position, fill='#2081E2')

# Draw the text "NFT" inside the box
font_size = 20 # Specify the desired font size for the NFT text
font_path = "./fonts/Inter-Bold.ttf" # Path to the Inter Bold font
font = ImageFont.truetype(font_path, font_size) # Load the desired font
draw.text((36, 33), "NFT", fill='white', font=font) # Draw the text inside the box
elif fallback:
# Draw a small box in the corner of the avatar
box_position = (30, 30, 150, 60)
draw.rectangle(box_position, fill='#FF0000')

# Draw the text "FALLBACK" inside the box
font_size = 20
font_path = "./fonts/Inter-Bold.ttf"
font = ImageFont.truetype(font_path, font_size)
draw.text((39, 33), "FALLBACK", fill='white', font=font)

# Display the user's nickname, or replace it with their ENS name they don't have a nickname
if 'name' in profile['records']:
name = profile['records']['name']
Expand All @@ -69,6 +134,7 @@ def generate_card(user):
font_path = "./fonts/Inter-Bold.ttf" # Path to the Inter Bold font
font = ImageFont.truetype(font_path, font_size) # Load the desired font
draw.text((280, 20), name, fill=(255, 255, 255), font=font) # Draw the text with the specified font

# Display the user's ENS name and ETH address (truncated)
if 'name' in profile and 'address' in profile:
truncated_address = f"{profile['address'][:6]}...{profile['address'][-4:]}"
Expand All @@ -77,6 +143,7 @@ def generate_card(user):
font_path = "./fonts/Inter-Regular.ttf" # Path to the Inter Regular font
font = ImageFont.truetype(font_path, font_size) # Load the desired font
draw.text((280, 80), idstring, fill=(255, 255, 255), font=font) # Draw the text with the specified font

# Display the user's description with text wrapping
description_y = 110
if 'description' in profile['records']:
Expand All @@ -98,6 +165,7 @@ def generate_card(user):
font = ImageFont.truetype(font_path, font_size)
draw.text((280, description_y), f"[no description given]", fill=(255, 255, 255), font=font)
description_y += font_size + 4

# Social media
social_media_spacing = 6
social_media_y = description_y + 10
Expand All @@ -112,6 +180,7 @@ def generate_card(user):
font_path = "./fonts/Inter-Regular.ttf"
font = ImageFont.truetype(font_path, font_size)
draw.text((280, social_media_y), f"[no Twitter given]", fill=(255, 255, 255), font=font)

social_media_y += font_size + social_media_spacing
if 'com.github' in profile['records']:
github = profile['records']['com.github']
Expand All @@ -124,6 +193,7 @@ def generate_card(user):
font_path = "./fonts/Inter-Regular.ttf"
font = ImageFont.truetype(font_path, font_size)
draw.text((280, social_media_y), f"[no GitHub given]", fill=(255, 255, 255), font=font)

social_media_y += font_size + social_media_spacing
if 'com.discord' in profile['records']:
discord = profile['records']['com.discord']
Expand All @@ -136,6 +206,7 @@ def generate_card(user):
font_path = "./fonts/Inter-Regular.ttf"
font = ImageFont.truetype(font_path, font_size)
draw.text((280, social_media_y), f"[no Discord given]", fill=(255, 255, 255), font=font)

social_media_y += font_size + social_media_spacing
if 'org.telegram' in profile['records']:
telegram = profile['records']['org.telegram']
Expand All @@ -148,6 +219,7 @@ def generate_card(user):
font_path = "./fonts/Inter-Regular.ttf"
font = ImageFont.truetype(font_path, font_size)
draw.text((280, social_media_y), f"[no Telegram given]", fill=(255, 255, 255), font=font)

social_media_y += font_size + social_media_spacing
if 'email' in profile['records']:
email = profile['records']['email']
Expand All @@ -160,6 +232,7 @@ def generate_card(user):
font_path = "./fonts/Inter-Regular.ttf"
font = ImageFont.truetype(font_path, font_size)
draw.text((280, social_media_y), f"[no email given]", fill=(255, 255, 255), font=font)

social_media_y += font_size + social_media_spacing
if 'url' in profile['records']:
url = profile['records']['url']
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Flask
requests
Pillow
Pillow
pydenticon

0 comments on commit 33ef67f

Please sign in to comment.