asciifier

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

asciifier.py (2128B)


      1 import os
      2 import sys
      3 import cv2
      4 import numpy as np
      5 from math import sqrt
      6 from PIL import Image
      7 
      8 
      9 # Get image fele name #
     10 
     11 if( len(sys.argv) >= 2 ):
     12 	image_name = sys.argv[1]
     13 else:
     14 	print("Terminated")
     15 	exit(0)
     16 
     17 
     18 # Get columns/rows #
     19 
     20 rows, columns = os.popen('stty size', 'r').read().split()
     21 print(columns)
     22 print(rows)
     23 
     24 
     25 # Set values #
     26 
     27 v0 = " "
     28 v1 = "-"
     29 v2 = "="
     30 v3 = ":"
     31 v4 = "%"
     32 v5 = "@"
     33 v6 = "#"
     34 
     35 st = True
     36 f_thresh = 110
     37 bilevel = True
     38 
     39 # Convert to bw #
     40 
     41 if bilevel:
     42 	imagef = Image.open(image_name)
     43 	imagef = imagef.convert('1')
     44 	imagef.save('result.png')	
     45 else:
     46 	imagef = Image.open(image_name)
     47 	# bw threshold #
     48 	thresh = 1 
     49 	fn = lambda x : 255 if x > thresh else 0
     50 	r = imagef.convert('L').point(fn, mode='1')
     51 	r.save('result.png')
     52 
     53 img = cv2.imread('result.png')
     54 width, height, lol = img.shape
     55 iteri = height / int(rows)
     56 iteri = int(iteri)
     57 iteri = iteri
     58 for r in range(0,img.shape[0],iteri):
     59 	print("\n", end="")
     60 	for c in range(0,img.shape[1],int(iteri-(iteri*45/100))):
     61 #for c in range(0,img.shape[1],int(iteri)):
     62 #	print("\n}", end="")
     63 #	for r in range(0,img.shape[0],int(iteri)):
     64 		myimg = img[r:r+int(iteri), c:c+int(iteri),:]
     65 		#cv2.imwrite(f"img{r}_{c}.png",img[r:r+30, c:c+30,:])
     66 		avg_color_per_row = np.average(myimg, axis=0)
     67 		avg_color = np.average(avg_color_per_row, axis=0)
     68 		#print(avg_color)
     69 		R,G,B= avg_color
     70 		value = (R+G+B)/3
     71 		#print(value, end =" ")
     72 
     73 		if st:
     74 			if value < 1 + f_thresh:
     75 				print(v0, end ="")
     76 			elif value < 5 + f_thresh:
     77 				print(v1, end ="")
     78 			elif value < 10 + f_thresh:
     79 				print(v2, end ="")
     80 			elif value < 25 + f_thresh:
     81 				print(v3, end ="")
     82 			elif value < 35 + f_thresh:
     83 				print(v4, end ="")
     84 			elif value < 40 + f_thresh:
     85 				print(v5, end ="")
     86 			else:
     87 				print(v6, end ="")
     88 		else:
     89 	
     90 			if value < 1 + f_thresh:
     91 				print("#", end ="")
     92 			elif value < 5 + f_thresh:
     93 				print("@", end ="")
     94 			elif value < 10 + f_thresh:
     95 				print("%", end ="")
     96 			elif value < 25 + f_thresh:
     97 				print(":", end ="")
     98 			elif value < 35 + f_thresh:
     99 				print("=", end ="")
    100 			elif value < 40 + f_thresh:
    101 				print("-", end ="")
    102 			else:
    103 				print(" ", end ="")
    104 print("\n")