Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent buffer around badge to mimic DIscord UI #173

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified crimsobot/data/img/roundping.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added crimsobot/data/img/roundping_buffer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 49 additions & 12 deletions crimsobot/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,23 +584,60 @@ def make_pingbadge_img(img: Image.Image, position: int) -> Image.Image:
ratio = max(width, height) / 500
img = img.resize((int(width / ratio), int(height / ratio)), resample=Image.BICUBIC)

# get new image size
width, height = img.size
size = int(width / 3)

if position == 1:
corner = (0, 0)
elif position == 2:
corner = (width - size, 0)
elif position == 3:
corner = (0, height - size)
elif position == 4:
corner = (width - size, height - size)

# will need the image's alpha mask later
alpha = img.split()[-1].convert('L')

# get original badge size and determine resize factor for badge and buffer
with Image.open(c.clib_path_join('img', 'roundping.png')) as badge:
width_badge, _ = badge.size # square image

if width <= height:
new_size = width // 3 # diameter/length of square
else:
new_size = height // 3

resize_factor = new_size / width_badge

# position of badge and buffer
if position == 1: # ↖️
rotation_angle = 0
shift = [0, 0]
elif position == 2: # ↗️
rotation_angle = -90
shift = [1, 0]
elif position == 3: # ↖↙️
rotation_angle = 90
shift = [0, 1]
elif position == 4: # ↘️
rotation_angle = 180
shift = [1, 1]
else:
raise BadArgument('Invalid position.')

badge_corner = (shift[0] * (width - new_size), shift[1] * (height - new_size))

# paste buffer
with Image.open(c.clib_path_join('img', 'roundping_buffer.png')) as buffer:
buffer_size, _ = buffer.size # square image

new_buffer_size = int(resize_factor * buffer_size)
buffer = buffer.resize((new_buffer_size, new_buffer_size), resample=Image.BICUBIC)
buffer = buffer.rotate(rotation_angle)
buffer = buffer.convert('L')

delta = new_buffer_size - new_size

buffer_corner = (badge_corner[0] - shift[0] * delta, badge_corner[1] - shift[1] * delta)
alpha.paste(buffer, buffer_corner)
img.putalpha(alpha)

# paste badge
with Image.open(c.clib_path_join('img', 'roundping.png')) as badge:
badge = badge.resize((size, size), resample=Image.BICUBIC)
img.paste(badge, corner, badge)
badge = badge.resize((new_size, new_size), resample=Image.BICUBIC)
img.paste(badge, badge_corner, badge)

return img

Expand Down
Loading