Skip to content
4 changes: 3 additions & 1 deletion img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def main(opt):
cell_height = 12
num_cols = int(width / cell_width)
num_rows = int(height / cell_height)
char_width, char_height = font.getsize(sample_character)
bbox = font.getbbox(sample_character)
char_width = bbox[2] - bbox[0]
char_height = bbox[3] - bbox[1]
out_width = char_width * num_cols
out_height = scale * char_height * num_rows
out_image = Image.new("L", (out_width, out_height), bg_code)
Expand Down
4 changes: 3 additions & 1 deletion img2img_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def main(opt):
cell_height = 12
num_cols = int(width / cell_width)
num_rows = int(height / cell_height)
char_width, char_height = font.getsize(sample_character)
bbox = font.getbbox(sample_character)
char_width = bbox[2] - bbox[0]
char_height = bbox[3] - bbox[1]
out_width = char_width * num_cols
out_height = scale * char_height * num_rows
out_image = Image.new("RGB", (out_width, out_height), bg_code)
Expand Down
2 changes: 1 addition & 1 deletion img2txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def main(opt):
if opt.mode == "simple":
CHAR_LIST = '@%#*+=-:. '
else:
CHAR_LIST = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
CHAR_LIST = r"""$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. """
num_chars = len(CHAR_LIST)
num_cols = opt.num_cols
image = cv2.imread(opt.input)
Expand Down
20 changes: 15 additions & 5 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@

def sort_chars(char_list, font, language):
if language == "chinese":
char_width, char_height = font.getsize("制")
bbox = font.getbbox("制")
char_width=bbox[2]-bbox[0]
char_height=bbox[3]-bbox[1]
elif language == "korean":
char_width, char_height = font.getsize("ㅊ")
bbox = font.getbbox("ㅊ")
char_width=bbox[2]-bbox[0]
char_height=bbox[3]-bbox[1]
elif language == "japanese":
char_width, char_height = font.getsize("あ")
bbox = font.getbbox("あ")
char_width=bbox[2]-bbox[0]
char_height=bbox[3]-bbox[1]
elif language in ["english", "german", "french", "spanish", "italian", "portuguese", "polish"]:
char_width, char_height = font.getsize("A")
bbox = font.getbbox("A")
char_width=bbox[2]-bbox[0]
char_height=bbox[3]-bbox[1]
elif language == "russian":
char_width, char_height = font.getsize("A")
bbox = font.getbbox("A")
char_width=bbox[2]-bbox[0]
char_height=bbox[3]-bbox[1]
num_chars = min(len(char_list), 100)
out_width = char_width * len(char_list)
out_height = char_height
Expand Down