Skip to content

Instantly share code, notes, and snippets.

@rien333
Last active December 6, 2017 15:55
Show Gist options
  • Save rien333/3ada36f7beb2cc0bc8bb06138c56eefe to your computer and use it in GitHub Desktop.
Save rien333/3ada36f7beb2cc0bc8bb06138c56eefe to your computer and use it in GitHub Desktop.
ascii live art on an andriod phone
import numpy as np
from scipy.misc import imresize
from PIL import Image
from os import system
from time import sleep
# Method to convert images(/image buffers?) to ascii art
# Change this to either use a shared library (.so) file or the python img2txt module
# stolen from img2txt.generate_grayscale_for_image
def img2ascii(pixels, height, width):
color = "MNHQ$OC?7>!:-;. " # grayscale # create a matrix that has this as elements or something?
string = ""
m = np.sum(pixels, axis=2) * 0.0208333
m = m.astype('uint8')
for h in range(height):
for w in range(width-1, 0, -1):
string += color[m[h, w]]
string += "\n"
# cap = cv2.VideoCapture(0) # Replace with andriod stuff
# Frame is 1080x720
div = 5
im_width = 1080 // div
im_height = 720 // div
# Term size (termux)
im_width = 87
im_height = 20
# Lower resolution with andriod stuff?
# cap.set(3, int(im_width))
# cap.set(4, int(im_height))
system("termux-camera-photo 'file.jpg'")
im = Image.open('file.jpg')
frame = np.array(im)
print(frame.shape)
print("Webcam resolution: %s x %s" % (frame.shape[1], frame.shape[0]))
sleep(2.0)
while(True):
system("termux-camera-photo 'file.jpg'")
im = Image.open('file.jpg')
frame = np.array(im)
frame = imresize(frame,(im_width, im_height), interp='nearest')
string = img2ascii(frame, im_height, im_width)
sys.stdout.write(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment