Skip to content

Commit

Permalink
Merge pull request #173 from crimsobot/pingbadge-update
Browse files Browse the repository at this point in the history
Transparent buffer around badge to mimic DIscord UI
  • Loading branch information
h-anjru authored Jul 30, 2024
2 parents 7be3bbc + 38ce1cb commit 1b1f14e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
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

0 comments on commit 1b1f14e

Please sign in to comment.