Last active
March 21, 2021 11:43
-
-
Save VicGrygorchyk/b2af7cd939a4ab887dcbd875b6d7bc7d to your computer and use it in GitHub Desktop.
See the image the way computer sees
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 pandas as pd | |
from PIL import Image | |
import numpy as np | |
# https://toppng.com/uploads/preview/emoji-emojis-emoticonos-emoticono-feliz-happy-reir-happy-face-emoji-115630245952vdezktgff.png | |
img = Image.open('index.png') | |
img.show() | |
img = img.resize((32, 32), ) | |
img.show() | |
img_as_arr = np.asarray(img) | |
print(img_as_arr) | |
print(img_as_arr.shape) | |
print(img.mode) | |
red = img_as_arr[:, :, :1] | |
green = img_as_arr[:, :, 1:2] | |
blue = img_as_arr[:, :, 2:3] | |
alpha = img_as_arr[:, :, 3:] | |
red = np.squeeze(red, 2) | |
print(red.shape) | |
# output must be (32, 32) | |
blue = np.squeeze(blue, 2) | |
green = np.squeeze(green, 2) | |
alpha = np.squeeze(alpha, 2) | |
df_red = pd.DataFrame(red) | |
df_green = pd.DataFrame(green) | |
df_blue = pd.DataFrame(blue) | |
df_alpha = pd.DataFrame(alpha) | |
# 'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', | |
# 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', | |
# 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', | |
# 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', | |
# 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', | |
# 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', | |
# 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', | |
# 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', | |
# 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', | |
# 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', | |
# 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', | |
# 'gist_yarg', | |
# 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', | |
# 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', | |
# 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', | |
# 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', | |
# 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'twilight', 'twilight_r', | |
# 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r' | |
style_r = df_red.style.set_properties(**{'font-size': '8pt'}).background_gradient('Reds_r') | |
style_g = df_green.style.set_properties(**{'font-size': '8pt'}).background_gradient('Greens_r') | |
style_b = df_blue.style.set_properties(**{'font-size': '8pt'}).background_gradient('Blues_r') | |
style_a = df_alpha.style.set_properties(**{'font-size': '8pt'}).background_gradient('Greys') | |
with open('image.html', 'w') as html: | |
html.write(style_r.render()) | |
html.write(style_g.render()) | |
html.write(style_b.render()) | |
html.write(style_a.render()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment