|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Original script by @atluft with inspiration from http://link.medium.com/0D6TnUKTIab located at: |
| 4 | +# https://github.com/o2sh/onefetch/wiki/ASCII-Art-From-Image-Using-Python-Image-Library/9c454b390273ffedd60db9d525fb001f89d581b1 |
| 5 | + |
| 6 | +# lint with |
| 7 | +# flake8 --max-line-length 120 ASCII_art_from_image.py |
| 8 | +# black --diff --color ASCII_art_from_image.py |
| 9 | + |
| 10 | +from argparse import ArgumentParser |
| 11 | +from PIL import Image, ImageDraw |
| 12 | +from math import floor |
| 13 | + |
| 14 | +__version__ = "1.0.0" |
| 15 | + |
| 16 | +parser = ArgumentParser(description="convert an image file to ASCII art") |
| 17 | +requiredNamed = parser.add_argument_group("required argument") |
| 18 | +requiredNamed.add_argument( |
| 19 | + "-f", |
| 20 | + "--file", |
| 21 | + help="image file to convert to ASCII", |
| 22 | + required=True, |
| 23 | +) |
| 24 | +parser.add_argument( |
| 25 | + "-r", |
| 26 | + "--resolution", |
| 27 | + nargs=2, |
| 28 | + metavar=("height", "width"), |
| 29 | + help="set custom resolution, default 25 45", |
| 30 | + type=int, |
| 31 | + default=[25, 45], |
| 32 | +) |
| 33 | +parser.add_argument( |
| 34 | + "-s", |
| 35 | + "--save_image", |
| 36 | + help="optionally save ASCII to image file", |
| 37 | + action="store_true", |
| 38 | +) |
| 39 | +parser.add_argument( |
| 40 | + "-v", |
| 41 | + "--version", |
| 42 | + help="show version", |
| 43 | + action="version", |
| 44 | + version="ASCII_art_from_image.py {}".format(__version__), |
| 45 | +) |
| 46 | +args = parser.parse_args() |
| 47 | + |
| 48 | +image = Image.open(args.file) |
| 49 | +scaleFac = 1.0 |
| 50 | +charWidth = int(args.resolution[1]) |
| 51 | +charHeight = int(args.resolution[0]) |
| 52 | +w, h = image.size |
| 53 | +image = image.resize((charWidth, charHeight), Image.NEAREST) |
| 54 | +w, h = image.size |
| 55 | +pixels = image.load() |
| 56 | + |
| 57 | +outputImage = Image.new("RGB", (charWidth * w, charHeight * h), color=(0, 0, 0)) |
| 58 | +draw = ImageDraw.Draw(outputImage) |
| 59 | + |
| 60 | + |
| 61 | +def getSomeChar(h): |
| 62 | + chars = "-_:. "[::-1] |
| 63 | + charArr = list(chars) |
| 64 | + mul = len(charArr) / 256 |
| 65 | + return charArr[floor(h * mul)] |
| 66 | + |
| 67 | + |
| 68 | +for i in range(h): |
| 69 | + for j in range(w): |
| 70 | + # REF: https://stackoverflow.com/a/52667876 |
| 71 | + p = pixels[j, i] |
| 72 | + r, g, b = p[0], p[1], p[2] |
| 73 | + grey = int((r / 3 + g / 3 + b / 3)) |
| 74 | + pixels[j, i] = (grey, grey, grey) |
| 75 | + draw.text((j * charWidth, i * charHeight), getSomeChar(grey), fill=(r, g, b)) |
| 76 | + print(getSomeChar(grey), end="") |
| 77 | + print("") |
| 78 | + |
| 79 | +if charHeight > 25 or charWidth > 45: |
| 80 | + print( |
| 81 | + "WARNING: Custom resolution exceeds maximum 25*45 (height*width). See: \n" |
| 82 | + "https://github.com/o2sh/onefetch/blob/master/CONTRIBUTING.md#ascii-logo" |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def fileRename(file): |
| 87 | + fileSpilt = file.split(".") |
| 88 | + outputFile = ".".join((fileSpilt[0:-1])) + "-ascii." + fileSpilt[-1] |
| 89 | + return outputFile |
| 90 | + |
| 91 | + |
| 92 | +if args.save_image: |
| 93 | + asciiFile = fileRename(args.file) |
| 94 | + outputImage.save(asciiFile) |
| 95 | + print("INFO: image saved as {}".format(asciiFile)) |
0 commit comments