Last active
September 28, 2020 00:46
-
-
Save xziyue/fc257351669050f2da58ba69771771c5 to your computer and use it in GitHub Desktop.
"typing" with emojis in discord
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 sys | |
import numpy as np | |
from PIL import Image, ImageFont, ImageDraw, ImageEnhance | |
import matplotlib.pyplot as plt | |
# parameters | |
font_size = 12 | |
threshold = 0.9 | |
emoji = ':a_:' | |
emoji_width = 6 | |
show_rendered_text = False | |
gradient_emoji = ':a{}:' | |
gradient_image = True | |
gradient_levels = 10 | |
gradient_range = (0.2, 1.0) | |
gradient_ticks = np.linspace(gradient_range[0], gradient_range[1], gradient_levels) | |
if sys.platform == 'win32': | |
font_path = os.path.join('C:' + os.path.sep, 'Windows', 'Fonts', 'simsun.ttc') | |
elif sys.platform == 'linux': | |
font_path = os.path.join('usr', 'share', 'fonts', 'opentype', 'NotoSerifCJK-Regular.ttc') | |
else: | |
raise RuntimeError('unknown platform "{}"'.format(sys.platform)) | |
print('try to load font "{}"'.format(font_path)) | |
image_font = ImageFont.truetype(font_path, font_size) | |
def generate_gradient_image(input_filename, output_format): | |
image = Image.open(input_filename) | |
gradient_ticks = np.linspace(gradient_range[0], gradient_range[1], gradient_levels) | |
brightness = ImageEnhance.Brightness(image) | |
for ind, level in enumerate(gradient_ticks): | |
new_img = brightness.enhance(level) | |
new_img.save(output_format.format(ind)) | |
def emoji_write(text): | |
image_size = image_font.getsize(text) | |
image = Image.new('L', image_size, 255) | |
draw = ImageDraw.Draw(image) | |
draw.text((0, 0), text, font=image_font) | |
image = np.asarray(image).astype(np.float32) / 255.0 | |
if show_rendered_text: | |
plt.imshow(image) | |
plt.show() | |
lines = [] | |
if gradient_image: | |
# invert pixel intensity | |
image = 1.0 - image | |
for i in range(image.shape[0]): | |
line = '' | |
for j in range(image.shape[1]): | |
if image[i,j] > gradient_range[0]: | |
index = np.searchsorted(gradient_ticks, image[i,j]) | |
line += gradient_emoji.format(index) | |
else: | |
line += ' ' * emoji_width | |
lines.append(line.rstrip()) | |
else: | |
fill_region = image < threshold | |
for i in range(fill_region.shape[0]): | |
line = '' | |
for j in range(fill_region.shape[1]): | |
if fill_region[i,j]: | |
line += emoji | |
else: | |
line += ' ' * emoji_width | |
lines.append(line.rstrip()) | |
start_index = 0 | |
for line in lines: | |
if all(map(lambda x : x == ' ', line)): | |
start_index += 1 | |
else: | |
break | |
for i in range(start_index, len(lines)): | |
lines[i] = '|' + lines[i] | |
return '\n'.join(lines[start_index:]) | |
#generate_gradient_image('smile.png','a{}.png') | |
result = emoji_write('hi') | |
with open('emoji.txt', 'w') as outfile: | |
outfile.write(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment