diff --git a/img2img.py b/img2img.py index 06936104..3e02b113 100644 --- a/img2img.py +++ b/img2img.py @@ -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) diff --git a/img2img_color.py b/img2img_color.py index 86a0768e..3f91f382 100644 --- a/img2img_color.py +++ b/img2img_color.py @@ -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) diff --git a/img2txt.py b/img2txt.py index 44bda99e..8099cd83 100644 --- a/img2txt.py +++ b/img2txt.py @@ -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) diff --git a/utils.py b/utils.py index b1ea6d9c..4fb6717b 100644 --- a/utils.py +++ b/utils.py @@ -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