Skip to content

Commit

Permalink
use argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed May 6, 2023
1 parent d12db39 commit ab4e9e0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 49 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.formatting.provider": "none"
}
79 changes: 30 additions & 49 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,99 +1,83 @@

#!/usr/bin/env python3
from tkinter import *
from PIL import Image
import argparse
import os
from tkinter import *

from PIL import Image
from tqdm import tqdm
import sys


def reduce_resolution(image_path, new_height):
with Image.open(image_path) as img:

width, height = img.size

reduction_factor = new_height / height

new_width = int(width * reduction_factor)

img = img.resize((new_width, new_height), resample=Image.BOX)

return img


def print_help():
print("Usage: ascii-art-generator [image_path] [height] \n --help or -h for help")
raise SystemExit()

def convert2D(liste):

list2d =[]
list2d = []

lines = []

ii =0
ii = 0

for i in liste:

if ii >= im.width-1:
ii=0
ii = 0
list2d.append(lines)
lines = []
else:
lines.append(i)
#print(lines)
ii+=1
# print(lines)
ii += 1
lines.append(i)
list2d.append(lines)
return list2d




if len(sys.argv) >= 2:
if(sys.argv[1] == "--help" or sys.argv[1] == "-h"):
print_help()
else:
if len(sys.argv) == 3:
img_filename = os.path.abspath(sys.argv[1])
img_height = int(sys.argv[2])
parser = argparse.ArgumentParser(
description='Transform an image into asciis characters.')
parser.add_argument('-p', '--picture',
help='Picture file to use.', required=True)
parser.add_argument(
'-he', '--height', help='Ascii art result height (In numbers of characters)', required=False, default=500)
args = parser.parse_args()

else:
print_help()
else:
print_help()

img_filename = args.picture
img_height = int(args.height)

#try:

im = reduce_resolution(img_filename, img_height).convert("L")



pixelles = convert2D(list(im.getdata()))


CHARS = ['ㅤㅤㅤ', '+++', '***', '===', '%%%', '###', '@@@', '&&&', '$$$', 'MMM', 'WWW', '888', '▌▌▌', '▬▬▬', '▒▒▒', '███', '▓▓▓']

CHARS = ['ㅤㅤㅤ', '+++', '***', '===', '%%%', '###', '@@@', '&&&',
'$$$', 'MMM', 'WWW', '888', '▌▌▌', '▬▬▬', '▒▒▒', '███', '▓▓▓']


result=""
result = ""

test =0
test = 0

for i in tqdm( range(0, len(pixelles)), desc="Loading..."):

for i in tqdm(range(0, len(pixelles)), desc="Loading..."):

result += "\n"



for ii in range(0, len(pixelles[i])):
result += CHARS[int(pixelles[i][ii]//(256/len(CHARS)))]






root = Tk()
Expand All @@ -104,14 +88,13 @@ def convert2D(liste):
root.geometry("700x350")



root.grid_columnconfigure(0, weight=500)
root.grid_rowconfigure(0, weight=500)


text = Text(root, height=im.height,wrap=NONE)
text = Text(root, height=im.height, wrap=NONE)
text.grid(row=0, column=0, sticky=EW)
text.config(font=('Helvetica bold',1))
text.config(font=('Helvetica bold', 1))


scrollbarVert = Scrollbar(root, orient='vertical', command=text.yview)
Expand All @@ -125,8 +108,6 @@ def convert2D(liste):
text['xscrollcommand'] = scrollbarHor.set




text.insert(1.0,result)
text.insert(1.0, result)

root.mainloop()

0 comments on commit ab4e9e0

Please sign in to comment.