commit 081c562bb42f4b01fd31f6b2a1e9a869509d9ba4
Author: MichaelDim02 <31562759+MichaelDim02@users.noreply.github.com>
Date: Wed, 10 Feb 2021 22:46:07 +0000
Add files via upload
Diffstat:
A | asciier.py | | | 103 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 103 insertions(+), 0 deletions(-)
diff --git a/asciier.py b/asciier.py
@@ -0,0 +1,103 @@
+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 = "#"
+
+
+# Convert to bw #
+
+bilevel = False
+
+if bilevel:
+ imagef = Image.open(image_name)
+ imagef = imagef.convert('1')
+ imagef.save('result.png')
+else:
+ imagef = Image.open(image_name)
+ 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*40/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 =" ")
+
+ st = True
+ if st:
+ if value < 1:
+ print(v0, end ="")
+ elif value < 5:
+ print(v1, end ="")
+ elif value < 10:
+ print(v2, end ="")
+ elif value < 25:
+ print(v3, end ="")
+ elif value < 35:
+ print(v4, end ="")
+ elif value < 40:
+ print(v5, end ="")
+ else:
+ print(v6, end ="")
+ else:
+
+ if value < 1:
+ print("#", end ="")
+ elif value < 5:
+ print("@", end ="")
+ elif value < 10:
+ print("%", end ="")
+ elif value < 25:
+ print(":", end ="")
+ elif value < 35:
+ print("=", end ="")
+ elif value < 40:
+ print("-", end ="")
+ else:
+ print(" ", end ="")
+print("\n")