Last active
May 28, 2020 00:33
-
-
Save yamitzky/efbf19e946b1fc5c4004ae628f1202f2 to your computer and use it in GitHub Desktop.
Slackの透過絵文字に白背景をつけます
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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