ART_TELEPHONE = ''' ..--""""----.. .-" ..--""""--.j-. .-" .-" .--.""--.. .-" .-" ..--"-. \/ ; .-" .-"_.--..--"" ..--' "-. : .' .' / `. \..--"" __ _ \ ; :.__.-" \ / .' ( )"-. Y ; ;: ( ) ( ). \ .': /:: : \ \ .'.-"\._ _.-" ; ; ( ) .-. ( ) \ " `.""" .j" : : \ ; ; \ bug /"""""/ ; ( ) "" :.( ) \ /\ / : \ \`.: _ \ : `. / ; `( ) (\/ :" \ \ \ `. : "-.(_)_.' t-' ; \ `. ; ..--": `. `. : ..--"" : `. "-. ; ..--"" ; `. "-.:_..--"" ..--" `. : ..--"" "-. : ..--"" "-.;_..--"" ''' ART_PHONE_SM1 = """ .----------------. / _H______H_ \@, \____/ \____/ @, / \ `@ | LI LI LI | ,@ | LI LI LI | ,@' | LI LI LI | ,@' | LI LI LI |@@' jgs \ /' `----------' """ ART_ROTARY2=""" _______________ / \ | .---------. |@ '---' .-----. '---'@ .' /6 5_4 3\ '. @ | |7 /...\ 2| | @ | |8 \___/ 1| | @ | \_9_0_)\/ | @@ /==|_____________|@@@@ H-------------------@@ H ) || || ( @@ H / || || \ @ H |----''---''----| =/ |_______________| """ ART_KEY = """ 8 8 8 8 ,ooo. 8a8 8a8 oP ?b d888a888zzzzzzzzzzzzzzzzzzzz8 8b `""^""' ?o___oP' """ ART_OLDPHONE = """ __ /` _`\ | (_()| .-. \_ _/_/ \ ||=[_] | || | | | ||/ \ | ||`---' / .--'||-.___.' /` .-||-. '-/`.____.`\ jgs '.______.' """ ART_OLDPHONE2=""" _|~|/| ( | | | /_|_|\| | | | |~| | | | | | | | |-| | | \ | |__| |_|_ / ~-_ / ~-_ |___________| """ ART_ROTARY = """ _...----..._ ,-' ,-. `-. ,' ,-. ( 4 ) ,-. `. ,' ( 5 ) `-' ( 3 ) `. / ,-. `-',-'' ``-.`-' ,-. \ / ( 6 ) ,' `. ( 2 ) \ : `-' / FEUER \ `-' : | ,-. : ________ : ,-. | |( 7 ) | |________| | ( 1 )| | `-' : ; `-' | : ,-. \ NOTRUF / ; \ ( 8 ) `. ,'(`. / \ `-' ,-.`-..__..-' \ `-./ `. ( 9 ) ,-. \ ,' `. `-' ( 0 ) ,'` `-._ `-' _.-' ```----''' SSt """ ART_PHONE_DIAGRAM = """ ________ .' / / ) / /##/ /| / `--' / | /__ __ __ / | //_//_//_// / __ //_//_//_// / \`.___ Listening end //_//_//_// / //_//_//_// /__ / / / \`.___ Buttons / .-. / / / /#/ / / / `-' / /__ / .====. / / \`.___ Speaking end |`--------' / \ , .'__ `-//----' \`.___ Disconnect button // """ ART_OLDPHONE3 = """ __ _ .: .' '. /: / \_ ;: ; ,-'/`:\ |: | | |():| ;: ; '-.\_:/ \: \ /` ':_'._.' || /__\ .---. {====} .' _,"-,__|:: | / ((O)=;--.:: | ; `|: | |:: | | |: | |:: | | |: | |:: | | |: | |:: | | |: | |:: | | |: | |:: | | /:'__\ |:: | | [______]|:: | | `----` |:: |__ | _.--|:: | ''--._ ; .' __{====}__ '. \ .'_.-'._ `""` _.'-._ '. '--'/` `''''` `\ '.__ jgs '._ _.' `""--......--""` """ ART_OLDPHONE4 = """ __ /` _`\ | (_()| .-. \_ _/_/ \ ||=[_] | || | | | ||/ \ | ||`---' / .--'||-.___.' /` .-||-. '-/`.____.`\ jgs '.______.' """ ART_PAYPHONE = """ _________________ / __ \ | (__) | | | | .-----. .--. | | | | / \ | | '-----' \ / | | | | | | LI LI LI | | | | LI LI LI | | |Oo | LI LI LI | | |`Oo | LI LI LI | | | Oo | | | | Oo | .------. / \ | oO | | | \ / | Oo | '------' '-oO | oO | .---Oo | Oo | || ||`Oo oO | |'--'| | OoO | '----' | jgs \_________________/ """ #### # code ### from PIL import Image ASCII_CHARS = [ '#', '?', '%', '.', 'S', '+', '.', '*', ':', ',', '@'] def scale_image(image, new_width=100): """Resizes an image preserving the aspect ratio. """ (original_width, original_height) = image.size aspect_ratio = original_height/float(original_width) new_height = int(aspect_ratio * new_width) new_image = image.resize((new_width, new_height)) return new_image def convert_to_grayscale(image): return image.convert('L') def map_pixels_to_ascii_chars(image, range_width=25): """Maps each pixel to an ascii char based on the range in which it lies. 0-255 is divided into 11 ranges of 25 pixels each. """ pixels_in_image = list(image.getdata()) pixels_to_chars = [ASCII_CHARS[pixel_value//range_width] for pixel_value in pixels_in_image] return "".join(pixels_to_chars) def convert_image_to_ascii(image, new_width=50): image = scale_image(image) image = convert_to_grayscale(image) pixels_to_chars = map_pixels_to_ascii_chars(image) len_pixels_to_chars = len(pixels_to_chars) image_ascii = [pixels_to_chars[index: index + new_width] for index in range(0, len_pixels_to_chars, new_width)] return "\n".join(image_ascii) def handle_image_conversion(image_filepath): image = None try: image = Image.open(image_filepath) except Exception as e: # print "Unable to open image file {image_filepath}.".format(image_filepath=image_filepath) # print e return image_ascii = convert_image_to_ascii(image) print(image_ascii) if __name__=='__main__': import sys image_file_path = sys.argv[1] handle_image_conversion(image_file_path)