asciifier

Script to turn images into ASCII art
git clone git://mcdim.xyz/asciifier.git
Log | Files | Refs | README

asciifier.py (1839B)


      1 import os
      2 import sys
      3 import cv2
      4 import argparse
      5 import numpy as np
      6 from math import sqrt
      7 from PIL import Image
      8 
      9 
     10 parser = argparse.ArgumentParser()
     11 parser.add_argument("-f", help="image file")
     12 parser.add_argument("-t", help="brightness threshold", default="100")
     13 parser.add_argument("-i", help="invert colors", action="store_true")
     14 
     15 #Declaring Argument Variables
     16 
     17 args = parser.parse_args()
     18 st = args.i
     19 f_thresh = int(args.t)
     20 image_name = args.f
     21 
     22 if not image_name:
     23     exit(1)
     24 
     25 
     26 # Get columns/rows
     27 
     28 rows, columns = os.popen("stty size", "r").read().split()
     29 print(columns)
     30 print(rows)
     31 
     32 
     33 # Set values
     34 v0 = " "
     35 v1 = "-"
     36 v2 = "="
     37 v3 = ":"
     38 v4 = "%"
     39 v5 = "#"
     40 v6 = "@"
     41 
     42 
     43 img = cv2.imread(image_name)
     44 
     45 width, height, lol = img.shape
     46 iteri = height / int(rows)
     47 iteri = int(iteri)
     48 iteri = iteri
     49 for r in range(0,img.shape[0],iteri):
     50 
     51 	print("\n", end="")
     52 
     53 	for c in range(0,img.shape[1],int(iteri-(iteri*45/100))):
     54 
     55 		myimg = img[r:r+int(iteri), c:c+int(iteri),:]
     56 		avg_color_per_row = np.average(myimg, axis=0)
     57 		avg_color = np.average(avg_color_per_row, axis=0)
     58 
     59 		R,G,B= avg_color
     60 		value = (R+G+B)/3
     61 
     62 		if st:
     63 			if value < 1 + f_thresh:
     64 				print(v0, end ="")
     65 			elif value < 5 + f_thresh:
     66 				print(v1, end ="")
     67 			elif value < 10 + f_thresh:
     68 				print(v2, end ="")
     69 			elif value < 25 + f_thresh:
     70 				print(v3, end ="")
     71 			elif value < 35 + f_thresh:
     72 				print(v4, end ="")
     73 			elif value < 40 + f_thresh:
     74 				print(v5, end ="")
     75 			else:
     76 				print(v6, end ="")
     77 		else:
     78 	
     79 			if value < 1 + f_thresh:
     80 				print("#", end ="")
     81 			elif value < 5 + f_thresh:
     82 				print("@", end ="")
     83 			elif value < 10 + f_thresh:
     84 				print("%", end ="")
     85 			elif value < 25 + f_thresh:
     86 				print(":", end ="")
     87 			elif value < 35 + f_thresh:
     88 				print("=", end ="")
     89 			elif value < 40 + f_thresh:
     90 				print("-", end ="")
     91 			else:
     92 				print(" ", end ="")
     93 
     94 print("\n")