From 2940c5057908d611c23cc3edbb15addeba11ed5e Mon Sep 17 00:00:00 2001 From: theboringkid Date: Fri, 28 Oct 2022 01:42:48 +0200 Subject: [PATCH] Intial Commit --- .gitignore | 2 ++ README.md | 17 ++++++++--------- ascii.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100755 ascii.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..225c381 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.png +*.jpg diff --git a/README.md b/README.md index 96dc2d4..56d122b 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ -# ImageToASCII - -Converts an Image into ASCII characters that look like that image. +# ImageToASCII -[REQUIREMENTS] -Python Pillow module -install with: -pip install Pillow (requires python-pip) +**Converts an Image into ASCII characters that look like that image.** -[USAGE] -python ascii.py [image path] \ No newline at end of file +### REQUIREMENTS ### +Python Pillow module - install with: +```console pip install Pillow ``` (requires python-pip) + +### USAGE ### +```console python ascii.py [image path]``` \ No newline at end of file diff --git a/ascii.py b/ascii.py new file mode 100755 index 0000000..9b1378d --- /dev/null +++ b/ascii.py @@ -0,0 +1,46 @@ +from PIL import Image +import sys + +if not sys.argv[1:]: + print("ERROR: No input given") + sys.exit(1) + +# list of characters sorted from bright to dark +characters = ['$','@','B','%','8','&','W','M','#','*','o','a','h','k','b','d','p','q','w','m','Z','O','0','Q','L','C','J','U','Y','X','z','c','v','u','n','x','r','j','f','t','/','\\','|','(',')','1','{','}','[',']','?','-','_','+','~','<','>','i','!','l','I',';',':',',','"','^','`','.',' '] + +def map(in_min, in_max,out_min,out_max,x): + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min + +def brightness(r,b,g): + avg= (r+b+g)/3 + return avg + +# print("Please enter Image Name if it if in the same Folder as the Python File, otherwise specify the path to the file!") +# imgpath=input() +image = Image.open(sys.argv[1:][0]) + +imagewith, imageheight = image.size #get size of image + +print("Please type the desired resolution on the x(width) Axis!(e.g. 150)") +resolution = int(input()) +# values to scale down the image +divisor = (int(imagewith/resolution))+1 +outwith = int(imagewith/divisor) +outheight = int(imageheight/divisor) + +# create an empty 2D Matrix to store the ASCII characters im +matrix = [[' ' for x in range(outheight)] for x in range(outwith)] + +#write the right characters in the matrix +for i in range(0,outheight*divisor,divisor): #do steps as big as the divisor, to skip as much pixels as needed for scaling + for j in range(0,outwith*divisor,divisor): #only until outsize*devisor, because that we mapped devisor input pixels to one output pixel and because of rounding we cannot map i.e. 38 when divisor is 5 and can only go up to outsize*divisor + xy= j, i + colors = image.getpixel(xy) + charnum = int(map(0,255,len(characters)-1,0,brightness(colors[0],colors[1],colors[2]))) + matrix[int(j/divisor)][int(i/divisor)]=characters[charnum] + +#print the matrix +for i in range(0,outheight,2): + for j in range(outwith): + print(matrix[j][i], end = "") #print without a new line at the end + print() #new line \ No newline at end of file