From c97dc711309b5045b17915e606df1e3a2f5a3f97 Mon Sep 17 00:00:00 2001 From: hgw Date: Mon, 2 Oct 2023 14:07:21 +0000 Subject: [PATCH] Make banter more universal, simplify image download and processing --- lib/banter/banter.py | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/lib/banter/banter.py b/lib/banter/banter.py index f4a01df..e70aa9d 100755 --- a/lib/banter/banter.py +++ b/lib/banter/banter.py @@ -14,34 +14,14 @@ import glob, os import requests def main(imgPath, delay, ASCIIWIDTH, COLORCHAR, FILLER, fileType): - if os.path.exists("image.png"): - os.remove("image.png") - if os.path.exists("image.jpg"): - os.remove("image.jpg") - if os.path.exists("image.webp"): - os.remove("image.webp") - if os.path.exists("image.jpeg"): - os.remove("image.jpeg") - if os.path.exists("output.txt"): - os.remove("output.txt") + if os.path.exists("image."+fileType): + os.remove("image."+fileType) if validators.url(imgPath) == True: - print('URL') - print('Downloading image to ' + "/home/node/app/image." + fileType) - #wget.download(imgPath, "/home/node/app/image." + fileType) headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0'} imagefile = requests.get(imgPath, headers=headers) open("/home/node/app/image." + fileType, "wb").write(imagefile.content) - if fileType == "png": - print('PNG') - imgPath = "/home/node/app/image.png" - if fileType == "jpg": - print('JPG') - imgPath = "/home/node/app/image.jpg" - if fileType == "webp": - print('WEBP') - imgPath = "/home/node/app/image.webp" - im = Image.open(imgPath, 'r') + im = Image.open("/home/node/app/image."+fileType, 'r') im = ImageOps.scale(im, ASCIIWIDTH / im.width) width, height = im.size pixel_values = list(im.getdata()) @@ -63,8 +43,6 @@ def main(imgPath, delay, ASCIIWIDTH, COLORCHAR, FILLER, fileType): currentPixel+=1 print("".join(line)) - with open("output.txt", "a") as f: - print("".join(line), file=f) if delay: time.sleep(delay) sys.stdout.flush()