asciifier

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

commit da838413b5587c82d4cdb7244db7f03f6423b35a
parent 753f70af7453574c76bd68c42ce3229168eb1585
Author: Michael Constantine Dimopoulos <mk@mcdim.xyz>
Date:   Sun, 19 Jun 2022 07:38:15 +0000

Updated asciifier.py

Diffstat:
Masciifier.py | 66++++++++++++++++++++++++++++--------------------------------------
1 file changed, 28 insertions(+), 38 deletions(-)

diff --git a/asciifier.py b/asciifier.py @@ -1,74 +1,63 @@ import os import sys import cv2 +import argparse import numpy as np from math import sqrt from PIL import Image -# Get image fele name # +parser = argparse.ArgumentParser() +parser.add_argument("-f", help="image file") +parser.add_argument("-t", help="brightness threshold", default="100") +parser.add_argument("-i", help="invert colors", action="store_true") -if( len(sys.argv) >= 2 ): - image_name = sys.argv[1] -else: - print("Terminated") - exit(0) +#Declaring Argument Variables +args = parser.parse_args() +st = args.i +f_thresh = int(args.t) +image_name = args.f -# Get columns/rows # +if not image_name: + exit(1) -rows, columns = os.popen('stty size', 'r').read().split() + +# Get columns/rows + +rows, columns = os.popen("stty size", "r").read().split() print(columns) print(rows) -# Set values # - +# 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') +v5 = "#" +v6 = "@" + + +img = cv2.imread(image_name) + 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: @@ -101,4 +90,5 @@ for r in range(0,img.shape[0],iteri): print("-", end ="") else: print(" ", end ="") + print("\n")