asciifier

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 823d213bac4143a1add665354c76a53c5c52e52c
parent 081c562bb42f4b01fd31f6b2a1e9a869509d9ba4
Author: MichaelDim02 <31562759+MichaelDim02@users.noreply.github.com>
Date:   Thu, 11 Feb 2021 00:26:27 +0000

Add files via upload
Diffstat:
Aasciifier.py | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 104 insertions(+), 0 deletions(-)

diff --git a/asciifier.py b/asciifier.py @@ -0,0 +1,104 @@ +import os +import sys +import cv2 +import numpy as np +from math import sqrt +from PIL import Image + + +# Get image fele name # + +if( len(sys.argv) >= 2 ): + image_name = sys.argv[1] +else: + print("Terminated") + exit(0) + + +# Get columns/rows # + +rows, columns = os.popen('stty size', 'r').read().split() +print(columns) +print(rows) + + +# Set values # + +v0 = " " +v1 = "-" +v2 = "=" +v3 = ":" +v4 = "%" +v5 = "@" +v6 = "#" + +st = True +f_thresh = 110 +bilevel = True + +# Convert to bw # + +if bilevel: + imagef = Image.open(image_name) + imagef = imagef.convert('1') + imagef.save('result.png') +else: + imagef = Image.open(image_name) + # bw threshold # + thresh = 1 + fn = lambda x : 255 if x > thresh else 0 + r = imagef.convert('L').point(fn, mode='1') + r.save('result.png') + +img = cv2.imread('result.png') +width, height, lol = img.shape +iteri = height / int(rows) +iteri = int(iteri) +iteri = iteri +for r in range(0,img.shape[0],iteri): + print("\n", end="") + for c in range(0,img.shape[1],int(iteri-(iteri*45/100))): +#for c in range(0,img.shape[1],int(iteri)): +# print("\n}", end="") +# for r in range(0,img.shape[0],int(iteri)): + myimg = img[r:r+int(iteri), c:c+int(iteri),:] + #cv2.imwrite(f"img{r}_{c}.png",img[r:r+30, c:c+30,:]) + avg_color_per_row = np.average(myimg, axis=0) + avg_color = np.average(avg_color_per_row, axis=0) + #print(avg_color) + R,G,B= avg_color + value = (R+G+B)/3 + #print(value, end =" ") + + if st: + if value < 1 + f_thresh: + print(v0, end ="") + elif value < 5 + f_thresh: + print(v1, end ="") + elif value < 10 + f_thresh: + print(v2, end ="") + elif value < 25 + f_thresh: + print(v3, end ="") + elif value < 35 + f_thresh: + print(v4, end ="") + elif value < 40 + f_thresh: + print(v5, end ="") + else: + print(v6, end ="") + else: + + if value < 1 + f_thresh: + print("#", end ="") + elif value < 5 + f_thresh: + print("@", end ="") + elif value < 10 + f_thresh: + print("%", end ="") + elif value < 25 + f_thresh: + print(":", end ="") + elif value < 35 + f_thresh: + print("=", end ="") + elif value < 40 + f_thresh: + print("-", end ="") + else: + print(" ", end ="") +print("\n")