|
| 1 | +import sys |
| 2 | +from PIL import Image, ImageDraw |
| 3 | + |
| 4 | +''' |
| 5 | +A grayscale pixel value is converted into one of these special characters |
| 6 | +''' |
| 7 | +ASCII_SET = ['@','#','$','%','?','*','+',';',':',',','.'] |
| 8 | + |
| 9 | +''' |
| 10 | +method resize() |
| 11 | + - takes parameter image and new_width |
| 12 | + - resizes the image into new_width maintaining the aspect aspect_ratio |
| 13 | + - returns this resized image, new width and new height |
| 14 | +''' |
| 15 | +def resize(image, new_width=100): |
| 16 | + (old_width, old_height) = image.size |
| 17 | + aspect_ratio = old_height/old_width |
| 18 | + new_height = int(aspect_ratio*new_width) |
| 19 | + new_image = image.resize((new_width,new_height)) |
| 20 | + return new_image, new_width, new_height |
| 21 | + |
| 22 | +''' |
| 23 | +method convertToGray() |
| 24 | + - takes parameter image |
| 25 | + - returns converted grayscale image |
| 26 | +''' |
| 27 | +def convertToGray(image): |
| 28 | + return image.convert('L') |
| 29 | + |
| 30 | +''' |
| 31 | +method PixelToAscii() |
| 32 | + - takes parameter image |
| 33 | + - returns a string with all image pixels converted to ascii special symbols from ASCII_SET |
| 34 | +''' |
| 35 | +def PixelToAscii(image, buckets=25): |
| 36 | + pixels = list(image.getdata()) |
| 37 | + new_pixels = [ASCII_SET[pixel_value//buckets] for pixel_value in pixels] |
| 38 | + return ''.join(new_pixels) |
| 39 | + |
| 40 | +''' |
| 41 | +method saveImage() |
| 42 | + - takes parameter string, new width and new height |
| 43 | + - writes the string in an image and saves the image as image.png |
| 44 | +''' |
| 45 | +def saveImage(ascii_str, new_width, new_height): |
| 46 | + image = Image.new(mode = "RGB", size = (new_width*11,new_height*11), color = "white") |
| 47 | + draw = ImageDraw.Draw(image) |
| 48 | + draw.multiline_text((0,0), ascii_str, fill=(0,0,0), align='center',spacing=0) |
| 49 | + image.save('image.png') |
| 50 | + |
| 51 | +''' |
| 52 | +method asciify() |
| 53 | + - takes parameter image path |
| 54 | + - calls the required functions to asciify the image |
| 55 | +''' |
| 56 | +def asciify(img_path): |
| 57 | + try: |
| 58 | + image = Image.open(img_path) |
| 59 | + except Exception: |
| 60 | + print("Unable to find image in", img_path) |
| 61 | + return |
| 62 | + image, new_width, new_height = resize(image) |
| 63 | + gray_image = convertToGray(image) |
| 64 | + ascii_char_list = PixelToAscii(gray_image) |
| 65 | + |
| 66 | + len_ascii_list = len(ascii_char_list) |
| 67 | + ascii_str = '' |
| 68 | + ascii_str = ''.join([ascii_str+i+i for i in ascii_char_list]) |
| 69 | + |
| 70 | + ascii_str = [ascii_str[index:index+2*new_width] for index in range(0, 2*len_ascii_list, 2*new_width)] |
| 71 | + ascii_str = "\n".join(ascii_str) |
| 72 | + |
| 73 | + saveImage(ascii_str, new_width, new_height) |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + if len(sys.argv)>1: |
| 77 | + img_path = str(" ".join(sys.argv[1:])) |
| 78 | + asciify(img_path) |
0 commit comments