Created
February 9, 2018 14:30
-
-
Save dnlcrl/7fe054edc05309393869df258190a229 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
from __future__ import print_function | |
from PIL import Image | |
from xtermcolor import colorize | |
from skimage.exposure import rescale_intensity | |
import argparse | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import scipy.misc | |
PIXEL = ' ' | |
def pack_rgb(pixel): | |
return (pixel[0] << 16) | (pixel[1] << 8) | pixel[2] | |
def decode_pixel(pixel): | |
rgb = pack_rgb(pixel) | |
print(colorize(PIXEL, ansi=0, bg=rgb), end='') | |
def scaled(img, width): | |
ratio = width / float(img.width) | |
return img.resize((width, int(img.height * ratio))) | |
def print_img(img, width=40): | |
img = rescale_intensity(np.array(img), out_range=(0, 255)) | |
img = Image.fromarray(img) | |
img = scaled(img.convert('RGB'), width) | |
plt.imshow(img) | |
plt.show() | |
for i, pixel in enumerate(img.getdata()): | |
decode_pixel(pixel) | |
if (i + 1) % width == 0: | |
print('') | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Print images in terminal using ANSI colors') | |
parser.add_argument('images', nargs='+', help='Images to print') | |
parser.add_argument('-w', '--width', type=int, | |
help='Set terminal width. Default: 80', default=80) | |
args = parser.parse_args() | |
for img in args.images: | |
img = scipy.misc.imread(img) | |
print_img(img, 40) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment