Created
July 7, 2017 03:06
-
-
Save susemeee/51f669a7cc0d4da60d4d87554daf7255 to your computer and use it in GitHub Desktop.
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
"""Converts image to IMAGE_SIZExIMAGE_SIZE slack emoji array""" | |
import sys | |
import numpy | |
from PIL import Image | |
EMOJI_MAP = [ | |
# enter custom emoji map here (dark one first) | |
# ':desktop_computer:', | |
# ':floppy_disk:', | |
# ':unicorn_face:', | |
] | |
EMOJI_LENGTH = len(EMOJI_MAP) | |
IMAGE_FILEPATH = './image.png' | |
IMAGE_SIZE = 20 | |
def jpg_image_to_array(image_path): | |
""" | |
Loads JPEG image into 3D Numpy array of shape | |
(width, height, channels) | |
""" | |
with Image.open(image_path).convert('L') as image: | |
image.thumbnail((IMAGE_SIZE, IMAGE_SIZE,), Image.NEAREST) | |
im_arr = numpy.array(image.getdata()) | |
im_arr = im_arr.reshape((image.size[1], image.size[0])) | |
return im_arr | |
def normalize_array(im_arr): | |
# normalize | |
th = int(256 / (EMOJI_LENGTH + 1)) | |
for i, row in enumerate(im_arr): | |
for j, val in enumerate(row): | |
for k in range(EMOJI_LENGTH): | |
if val <= th * (k + 1): | |
im_arr[i][j] = k | |
break | |
if val > th * EMOJI_LENGTH: | |
im_arr[i][j] = (EMOJI_LENGTH - 1) | |
return im_arr | |
if __name__ == '__main__': | |
pixels = normalize_array(jpg_image_to_array(IMAGE_FILEPATH)) | |
# print(ar) | |
# ar = [ | |
# [0,0,1,0,0,0,0], | |
# [0,0,1,0,0,0,0], | |
# [0,0,1,1,1,0,0], | |
# [1,1,1,1,1,1,0], | |
# [1,1,1,1,1,0,0], | |
# [1,1,1,1,1,1,0], | |
# [1,1,1,1,1,0,0], | |
# ] | |
for row in pixels: | |
for cell in row: | |
sys.stdout.write(EMOJI_MAP[cell]) | |
sys.stdout.write('\n') | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment