Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slackの透過絵文字に白背景をつけます
import os
import requests
from io import BytesIO
from PIL import Image
import numpy as np
TOKEN = os.environ['SLACK_TOKEN']
res = requests.get('https://slack.com/api/emoji.list',
headers={'Authorization': f'Bearer {TOKEN}'})
emojis = res.json()['emoji']
for name, url in emojis.items():
if not url.startswith('https://'):
continue
img_bytes = requests.get(url).content
img = Image.open(BytesIO(img_bytes))
img_arr = np.asarray(img)
if len(img_arr.shape) < 3 or img_arr.shape[2] != 4:
continue
transparent = img_arr[:, :, 3].min() == 0
gray = img_arr[:, :, :3].max(axis=2).astype(float)
gray *= (img_arr[:, :, 3] / 255)
dark = np.percentile(gray[img_arr[:, :, 3] > 0], 80) <= 80
if not dark or not transparent:
continue
img.save(f'backup/{name}.png')
white = Image.fromarray(np.ones(img_arr.shape, dtype=img_arr.dtype) * 255)
white.paste(img, (0, 0), img.split()[3])
white.save(f'result/{name}.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.