Last active
August 13, 2016 09:03
-
-
Save makersm/c4d434e917f6c182b47969faff6aa51d to your computer and use it in GitHub Desktop.
convert image to ascii text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import os | |
import re | |
import asyncio | |
from PIL import Image | |
def pixel_converter(bright): | |
if bright == 255: | |
val = ' ' | |
elif bright > 200: | |
val = '.' | |
elif bright > 150: | |
val = '-' | |
elif bright > 100: | |
val = '%' | |
elif bright > 50: | |
val = '#' | |
else: | |
val = '@' | |
return val | |
def asciiart_generator(input_path, output_path): | |
img = Image.open(input_path).convert('LA') | |
img.thumbnail((54, 54)) | |
width, height = img.size | |
px = img.load() | |
with open(output_path, 'w') as f: | |
for h in range(height): | |
for w in range(width): | |
f.write(pixel_converter(px[w, h][0])) | |
f.write('\n') | |
os.startfile(output_path) | |
def path_finder(): | |
pathes = list() | |
for root, dirnames, filenames in os.walk(os.path.dirname(os.path.abspath(__file__))): | |
for filename in filenames: | |
if not re.match('.*\.(jpg|png|jpeg|gif)$', filename, re.IGNORECASE): | |
continue | |
input_path = os.path.join(root, filename) | |
output_path = input_path + '.txt' | |
pathes.append((input_path, output_path)) | |
return pathes | |
def start(): | |
looper = asyncio.get_event_loop() | |
futures = list() | |
for input_path, output_path in path_finder(): | |
futures.append(looper.run_in_executor(None, functools.partial(asciiart_generator, input_path, output_path))) | |
looper.run_until_complete(asyncio.wait(futures)) | |
if __name__ == '__main__': | |
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.................................................................................................... | |
.................................................................................................... | |
.................................................................................................... | |
.................................................................................................... | |
.................................................................................................... | |
.................................................................................................... | |
...........................................................-------.................................. | |
.........................................................---------%-----............................ | |
.......................................................-----%%%%%%%%%%%%%--......................... | |
...............................................--.....----%%%%%%#%-----%%%%%-....................... | |
.................................--.............--...---%%%%%#%-.........-%#%-...................... | |
..............................--.............-......----%%%#%-.............-#%%-.................... | |
...................--.......---................--...--%%%#%-.................%#%-................... | |
..................--.......--....................--..-%%#%....................-%%-............----.. | |
..................-......---.......................-.-%#-.......................%#-.......----%%%%-- | |
.................-.......--.........................-.--.........................-#-....-----%%%%%%% | |
.......................---...........................-............................-#-.--------%%%%%% | |
.......................--....................------...-........-...........-.......%#-----------%%%% | |
......................--.......--%%..-..------#####%-.--...--..#-..........%-.--....%%-------------% | |
...-.................--....--%%##%%.-------------%%##-.--..-%.-#...........-%.%-.%-.-#-%%%%--------% | |
....-................--...%#%%--....-----------.........-..-#%%#%-.........-#%#%-#...%%%%%%%-------- | |
....-...............--....-........--------------%-----.--%##%%%%#%.......%#%%%###...-#%%%%%----%%%% | |
.....-..............--........-%%-.-------------#@%----.---...#- -#-......%-...-##%-..%%%%%#%%--###@ | |
.....-.............---........-@@#.-------------%@#-----.-....-.-#%........%%-..%--%...%#@@@@%%%#%%# | |
.....-.............--.........-##%.-----%%%-------------.--.--%#%-..........-%%%%-%#...-%@@@@@##@%%@ | |
%%-................--.......--....------@@@#------------.--%%%-...............--%%-.....%###@#%%#%#@ | |
%#%................--......-------------@@@#---------------.....................--......%#%%##%###@@ | |
...................--.....-------------.-#%-----------------.--................------...-###%%@%-### | |
...................--...-------------.........-------------..........----%#-...-------..-###--%---%% | |
...................--...-------------...........-----------.........-#####%.......-----.-%--------%% | |
...................-%-..--------------..........-----------..........----..----......---%%%%-----%%% | |
-....-.............---..--------------....-....-----%------......--.-.....--------.-----%%%%%-%%%%%% | |
-....-..............--..------------------------%%%%%%%------..-----------------------.-%%%%%%%%--%- | |
..----..............--..---------------------%%%%%%%%%%-------------------------------.%%%%%%%%----- | |
.----...............---.---------------------%%%%%%%%%%------------------------------.-#%----------- | |
-----................--.--------------------%%%%%%%%%%---%----------------------------#%%---------%- | |
---..................----------------------%%%%%%%%%%%--%----------------------------##%------------ | |
--....................---------------------%%%%%%%-----%%--------------------------%##%------------- | |
---...................------------------------%%%-----%%---------------------..--%###%%....--------- | |
---....................------------------------------%%-----------------------%%###%%%-...---------- | |
--......................--%-------------------------%%--%###%%%%%%--%%%###########%%%-....%#%--%#%.- | |
%-.......................---%%%----------------%%%%%%--%######@@@@##@@@@@########%%%--...-#%#%%#%%-- | |
%%........................-----%%%%%---%%-%%%%%%%%%---%##########@@############%%%----....%##--#@%-- | |
-%-..-..---...............----------------%%%%%-------###########@###########%%--.-.--....%##%-%%%-. | |
-------.---...............-------------------------..-%%%%%############%##%%--%--........-%--%%%-%-. | |
-----------...............------------%%%---%%%%--.....----------%##%-------------.......-%%%%-%#%-- | |
-----------................----------%%%----%%%%%%-....---------------------------.......--%%------- | |
..--------..................-......------%%--%%%%%%--..---------------------------......------------ | |
..--------.................---.....---..%%%%--%%%%%%%%--..--....------..----..--.......------------% | |
..-------................------...---..-%%%%----%%%--%%--.........---...----.........-...------%%##@ | |
..-----.................-------.---..--%%---------..-%%%---..........................###%%####@@@@@@ | |
........................-------.-...--%-------%-----%%%%---..........................#@@@@@@@@@@@@@@ | |
........................------.....--%---------%%---%%%%----.........................%@@@@@%--#@@@@@ | |
.......................----%--....-------------%%---%%%%%---.........................-#@@@#....%@@@@ | |
-.....................-----%-....-----------%%%%----%%-%%----...................--...-%%##%....-###% | |
%-...................-----%--....-----------%%------%%-%%-----.......................--%%%%....%##%% | |
%-..................------%--....-------------------%%%%%------...............--......--%%%-...###%% | |
%-..................-----%---.....--------------------%%%%%----...............--.....-%%%%#%..-@@@@# | |
%-.................----%%-.--.....------------------.---%%-----...............--.....#@@@@@%..%@@@@@ | |
%-.................------..-.....-------------------...--------...............-.....-@@@@@@%..#@@@@@ | |
%..................-----...-......-------------------....-----........----....-.....-@@@@@#...%@@@@@ | |
..................----.....-.....---------%%%--------.................------..-......%@@@#%----##### | |
..................---............---------%%%%-------.................-------........------.....---. | |
................................--------------------...........--.....---..--....................... | |
................................---------------%%---...........--.....-------....................... | |
...............................-------%%--------%%-...........---.....-----------................... | |
................................----------%%%%%%%--.........---------------------------............. | |
..............................------%%%%%%%%%%%%%--.......--------------------------................ | |
..............................-----%%%-...%%%%%%---.......----------.-------........................ | |
...............................----%%%-..-%%%%-----................................................. | |
...............................----%%%...-%%%%-----................................................. | |
--.............................----%%%...-%%%------................................................. | |
...............................----%%%...-%%%------................................................. | |
...............................----%%%----%%%%%%%--------........................................... | |
...............................---%%#%-%%%%#%%%%%---................................................ | |
...............................---%%---------....................................................... | |
.................................................................................................... | |
.................................................................................................... | |
.................................................................................................... |
Author
makersm
commented
Aug 13, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment