├── LICENSE ├── README.md ├── ascii-factory.py ├── github.jpg └── github.jpg.txt /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Kinman Covey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ascii-factory 2 | ============= 3 | 4 | **ascii-factory** is a Python program that takes an image file and converts it to ASCII art (as best as it can). Just a fun project that I wanted to try out. Will be returning to periodically as I can. 5 | 6 | The program will also tell you the average Red, Green, and Blue values in the image as well as the average color in the entire image. Might be helpful for some. 7 | 8 | Dependencies 9 | ============ 10 | 11 | **PIL** (Python Imaging Library) 12 | 13 | Usage 14 | ===== 15 | 16 | Very simple. Enter a filename, then hit enter. Then enter a value for the width and height to be scaled down by, keep it tight. Watch as it turns to ASCII art before you. 17 | 18 | Example 19 | ======= 20 | 21 | Passing in the `github.jpg` provided in the repo and using an x scale of 10 and a y scale of 20, we receive this beautiful output: 22 | ``` 23 | ........................................................ 24 | ........................................................ 25 | ........................................................ 26 | ........................................................ 27 | .......................-#########%...................... 28 | ..................####################:................. 29 | ...............##########################i.............. 30 | .............###############################............ 31 | ...........##################################%.......... 32 | ..........#######......#........'#......#######......... 33 | .........########.......................########........ 34 | ........#########.......................########'....... 35 | ........########.........................########....... 36 | .......I#######...........................#######....... 37 | .......########...........................#######....... 38 | .......########...........................#######....... 39 | ........########........................%########....... 40 | ........#########.......................#########....... 41 | .........##########..................`##########........ 42 | .........%####..%#######........%##############......... 43 | ...........#####..#####..........-############.......... 44 | ............:####.................##########............ 45 | ..............-########...........########.............. 46 | .................######..........%#####%................ 47 | .....................%#...........#`.................... 48 | ........................................................ 49 | ........................................................ 50 | ........................................................ 51 | ``` 52 | State of the art, I know. 53 | -------------------------------------------------------------------------------- /ascii-factory.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # ascii-factory 3 | # Kinman Covey 4 | # 3/2/2015 5 | 6 | from PIL import Image 7 | import sys 8 | 9 | run = True 10 | while run != False: 11 | # Get a filename, and a scale 12 | # Create the image object and print out its dimensions 13 | filename = raw_input("Enter filename (or stop to exit): ") 14 | if filename == "stop": 15 | run = False 16 | break 17 | 18 | while True: 19 | try: 20 | image = Image.open(filename) 21 | break 22 | except IOError: 23 | filename = raw_input("Incorrect filename, enter again (or stop to exit): ") 24 | 25 | print "\nThis next value is how much you want to scale each dimension down by (because the image is more than likely very large)." 26 | print "I've found scale_x = 10 and scale_y = 20 works well, but any 1x2 dimension would probably be fine." 27 | scale_x = input("Enter the down scale x value (5, 2, etc): ") 28 | scale_y = input("Enter the down scale y value (10, 4, etc): ") 29 | 30 | # Open text file for output 31 | text_file = open(filename + ".txt", "w") 32 | 33 | 34 | print "Image size: " + str(image.size) 35 | 36 | w, h = image.size 37 | 38 | obj = image.load() 39 | 40 | # Data section 41 | # List RGB vars 42 | r_list = [] 43 | g_list = [] 44 | b_list = [] 45 | 46 | # Average RGB vars 47 | r_avg = 0 48 | g_avg = 0 49 | b_avg = 0 50 | i_avg = 0 51 | 52 | # Minimum RGB vars 53 | r_min = 255 54 | g_min = 255 55 | b_min = 255 56 | 57 | # Overall count var 58 | count = 0 59 | 60 | print "Finding RGB floors..." 61 | # Find the minimum r, g, and b value for testing purposes 62 | # Found out picture that were really light were not decipherable 63 | # Added this to detect minimum of each and use that as the floor 64 | for i in range(0, h): 65 | for j in range(0, w): 66 | if len(obj[j,i]) == 3: 67 | r, g, b = obj[j,i] 68 | elif len(obj[j,i]) == 4: 69 | r, g, b, a = obj[j,i] 70 | if r < r_min: 71 | r_min = r 72 | 73 | if g < g_min: 74 | g_min = g 75 | 76 | if b < b_min: 77 | b_min = b 78 | 79 | # Analytical data 80 | if r > g and r > b: 81 | r_list.append(r) 82 | elif g > r and g > b: 83 | g_list.append(g) 84 | elif b > r and b > g: 85 | b_list.append(b) 86 | 87 | count += 1 88 | 89 | 90 | print "Finding averages..." 91 | #Find the averages 92 | for i in range(0, len(r_list)): 93 | r_avg += r_list[i] 94 | 95 | for i in range(0, len(g_list)): 96 | g_avg += g_list[i] 97 | 98 | for i in range(0, len(b_list)): 99 | b_avg += b_list[i] 100 | 101 | print len(r_list) 102 | 103 | i_avg = (r_avg + g_avg + b_avg) / count 104 | if len(r_list) > 0: 105 | r_avg = r_avg / len(r_list) 106 | if len(g_list) > 0: 107 | g_avg = g_avg / len(g_list) 108 | if len(b_list) > 0: 109 | b_avg = b_avg / len(b_list) 110 | 111 | print "Printing picture..." 112 | # And here's where the magic happens 113 | for i in range(0, h, scale_y): 114 | for j in range(0, w, scale_x): 115 | if len(obj[j,i]) == 3: 116 | r, g, b = obj[j,i] 117 | elif len(obj[j,i]) == 4: 118 | r, g, b, a = obj[j,i] 119 | 120 | if r <= r_min + 25 and g <= g_min + 25 and b <= b_min + 25: 121 | sys.stdout.write("#") 122 | text_file.write("#") 123 | elif r <= r_min + 50 and g <= g_min + 50 and b <= b_min + 50: 124 | sys.stdout.write("%") 125 | text_file.write("%") 126 | elif r <= r_min + 75 and g <= g_min + 75 and b <= b_min + 75: 127 | sys.stdout.write("I") 128 | text_file.write("I") 129 | elif r <= r_min + 100 and g <= g_min + 100 and b <= b_min + 100: 130 | sys.stdout.write("i") 131 | text_file.write("i") 132 | elif r <= r_min + 125 and g <= g_min + 125 and b <= b_min + 125: 133 | sys.stdout.write("!") 134 | text_file.write("!") 135 | elif r <= r_min + 150 and g <= g_min + 150 and b <= b_min + 150: 136 | sys.stdout.write("+") 137 | text_file.write("+") 138 | elif r <= r_min + 175 and g <= g_min + 175 and b <= b_min + 175: 139 | sys.stdout.write(":") 140 | text_file.write(":") 141 | elif r <= r_min + 200 and g <= g_min + 200 and b <= b_min + 200: 142 | sys.stdout.write("-") 143 | text_file.write("-") 144 | elif r <= r_min + 225 and g <= g_min + 225 and b <= b_min + 225: 145 | sys.stdout.write("'") 146 | text_file.write("'") 147 | elif r >= r_min + 250 and g >= g_min + 250 and b >= b_min + 250: 148 | sys.stdout.write(" ") 149 | text_file.write(" ") 150 | elif r > b and g > b and r > r_avg and g > g_avg: # Yellows 151 | sys.stdout.write("`") 152 | text_file.write("`") 153 | elif r > g and b > g and r > r_avg and b > b_avg: # Purps 154 | sys.stdout.write("%") 155 | text_file.write("%") 156 | elif g > r and b > r and g > g_avg and b > b_avg: # Cyans 157 | sys.stdout.write(";") 158 | text_file.write(";") 159 | elif r > g and r > b and r > r_avg: # Reds 160 | sys.stdout.write("$") 161 | text_file.write("$") 162 | elif g > r and g > b and g > g_avg: # Greens 163 | sys.stdout.write(",") 164 | text_file.write(",") 165 | elif b > g and b > r and b > b_avg: # Blues 166 | sys.stdout.write("\"") 167 | text_file.write("\"") 168 | else: 169 | sys.stdout.write(".") 170 | text_file.write(".") 171 | print 172 | text_file.write("\n") 173 | 174 | text_file.close() 175 | print "Avg R: " + str(r_avg) 176 | print "Avg G: " + str(g_avg) 177 | print "Avg B: " + str(b_avg) 178 | print "AVG RGB: " + str(i_avg) 179 | 180 | # We're done here 181 | print "\nThat's all folks!\n" 182 | 183 | -------------------------------------------------------------------------------- /github.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kin5/ascii-factory/10f4f9c9e43b79c0f9d18b1f3298dc93382665c5/github.jpg -------------------------------------------------------------------------------- /github.jpg.txt: -------------------------------------------------------------------------------- 1 | ........................................................ 2 | ........................................................ 3 | ........................................................ 4 | ........................................................ 5 | .......................:#########%...................... 6 | ..................####################+................. 7 | ...............##########################i.............. 8 | .............###############################............ 9 | ...........##################################%.......... 10 | ..........#######......#........-#......#######......... 11 | .........########.......................########........ 12 | ........#########.......................########-....... 13 | ........########.........................########....... 14 | .......I#######...........................#######....... 15 | .......########...........................#######....... 16 | .......########...........................#######....... 17 | ........########........................%########....... 18 | ........#########.......................#########....... 19 | .........##########..................'##########........ 20 | .........%####..%#######........%##############......... 21 | ...........#####..#####..........:############.......... 22 | ............+####.................##########............ 23 | ..............:########...........########.............. 24 | .................######..........%#####%................ 25 | .....................%#...........#'.................... 26 | ........................................................ 27 | ........................................................ 28 | ........................................................ 29 | --------------------------------------------------------------------------------