Last active
March 6, 2023 20:05
-
-
Save timgianitsos/c5d95ced433bb9e2a9b5074844b06c6b to your computer and use it in GitHub Desktop.
Display images on the command line using 4 shades of gray
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 numpy as np | |
__author__ = 'Tim Gianitsos' | |
def display_img(img): | |
''' | |
img: a 2D array of ints | |
''' | |
dark_black = '\u001B[40m ' | |
light_black = '\u001B[100m ' | |
dark_white = '\u001B[47m ' | |
light_white = '\u001B[107m ' | |
reset_color = '\u001B[0m' | |
percs = np.percentile(np.unique(img), (25, 50, 75)) | |
for r in range(img.shape[0]): | |
for c in range(img.shape[1]): | |
print( | |
dark_black if img[r, c] <= percs[0] else | |
light_black if img[r, c] <= percs[1] else | |
dark_white if img[r, c] <= percs[2] else light_white, | |
end="" | |
) | |
print(reset_color) | |
print() | |
if __name__ == '__main__': | |
try: | |
from sklearn import datasets | |
batch_size = 10 | |
imgs = datasets.load_digits().data[:batch_size].reshape( | |
batch_size, 8, 8 | |
) | |
except ModuleNotFoundError: | |
imgs = np.array([[ | |
[0., 0., 11., 12., 0., 0., 0., 0.,], | |
[0., 2., 16., 16., 16., 13., 0., 0.,], | |
[0., 3., 16., 12., 10., 14., 0., 0.,], | |
[0., 1., 16., 1., 12., 15., 0., 0.,], | |
[0., 0., 13., 16., 9., 15., 2., 0.,], | |
[0., 0., 0., 3., 0., 9., 11., 0.,], | |
[0., 0., 0., 0., 9., 15., 4., 0.,], | |
[0., 0., 9., 12., 13., 3., 0., 0.,], | |
]]) | |
for img in imgs: | |
display_img(img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment