banter/banter.py

57 lines
1.8 KiB
Python
Raw Permalink Normal View History

#!/bin/sh
2022-01-27 02:20:29 +00:00
"""'exec /usr/bin/env python3 -u "$0" "$@" #"""
__doc__ = "little script to make irc color art that will probably get you banned"
# whee, look at this sus shebang workaround to always cause
# unbuffered mode lol
2020-12-19 02:09:39 +00:00
2022-01-27 02:20:29 +00:00
import sys, time, argparse
from PIL import Image, ImageOps
2020-12-19 02:09:39 +00:00
from color import closestColor
2022-01-27 02:20:29 +00:00
def main(imgPath, delay, ASCIIWIDTH, COLORCHAR, FILLER, SHARPNESS):
im = Image.open(imgPath, "r")
im = ImageOps.scale(im, ASCIIWIDTH * SHARPNESS / im.width)
2021-01-01 19:02:12 +00:00
width, height = im.size
pixel_values = list(im.getdata())
currentPixel = 0
2022-01-27 02:20:29 +00:00
for y in range(0, height, 2 * SHARPNESS):
2021-01-01 19:02:12 +00:00
line = []
2022-01-27 02:20:29 +00:00
lastColor = 69420
2021-01-01 19:02:12 +00:00
for x in range(0, width, SHARPNESS):
2022-01-27 02:20:29 +00:00
color = closestColor(pixel_values[width * y + x])
2021-01-01 19:02:12 +00:00
if color == lastColor:
2022-01-27 02:20:29 +00:00
colorcode = ""
2021-01-01 19:02:12 +00:00
else:
2022-01-27 02:20:29 +00:00
colorcode = COLORCHAR.format(color, color)
line.append(colorcode + (FILLER[currentPixel % len(FILLER)]))
lastColor = color
currentPixel += 1
2021-01-01 19:02:12 +00:00
2022-01-27 02:20:29 +00:00
print("".join(line))
2021-01-01 19:02:12 +00:00
if delay:
time.sleep(delay)
if __name__ == "__main__":
parser = argparse.ArgumentParser("banter")
parser.add_argument("file")
2022-01-27 02:20:29 +00:00
parser.add_argument("-d", metavar="delay", default=0)
parser.add_argument("-w", metavar="width", default=80)
parser.add_argument("-s", metavar="sharpness", default=4)
parser.add_argument("--colorfmt", metavar="format", default="\\x03{},{}")
parser.add_argument("--filler", metavar="filler", default=".")
2021-01-01 19:02:12 +00:00
args = parser.parse_args()
main(
2022-01-27 02:20:29 +00:00
args.file,
float(args.d),
int(args.w),
args.colorfmt.encode().decode("unicode_escape"),
args.filler.encode().decode("unicode_escape"),
int(args.s),
)