Created
November 6, 2015 03:59
-
-
Save omad/d7fc32bc3bd67ce5413a to your computer and use it in GitHub Desktop.
Output an ASCII representation of a GDAL image to the terminal
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 | |
from osgeo import gdal | |
import warnings | |
import scipy.ndimage | |
@click.command(help="Print an image to the terminal ") | |
@click.option('--size', '-s') | |
@click.argument('filename', type=click.Path(exists=True, readable=True)) | |
def print_image(filename='', size=50): | |
""" | |
Output an ASCII representation of a GDAL image to the terminal | |
:param filename: | |
:return: | |
""" | |
chars = np.asarray(list(' .,:;irsXA253hMHGS#9B&@')) | |
character_size_ratio = 7 / 4.0 # width to height | |
output_height = float(size) | |
ds = gdal.Open(filename) | |
band = ds.GetRasterBand(1) | |
ar = band.ReadAsArray() | |
input_width, input_height = ar.shape | |
scale_factor = output_height / input_height | |
with warnings.catch_warnings(): | |
warnings.simplefilter("ignore") # We don't care that the output shape may have changed | |
image = scipy.ndimage.interpolation.zoom(ar, (scale_factor, scale_factor * character_size_ratio), order=0) | |
image *= (22.0 / image.max()) | |
image = image.clip(0) | |
print(("\n".join(("".join(r) for r in chars[image.astype(int)])))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment