This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage2textimage.py
73 lines (57 loc) · 1.87 KB
/
image2textimage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from v2t import frameToText, loadFrame
from t2v import textToFrame
import argparse
import sys
import numpy as np
import cv2
THRESHOLD = 90
CHARS = ['W', '#', 'R', 'E', '8', 'x', 's', 'i', ';', ',', '.', ' ']
PALETTE = np.arange(len(CHARS))
FONT = '/Users/yusuke/Library/Fonts/Ricty-Regular.ttf'
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('input', nargs='?')
parser.add_argument('output', nargs='?')
parser.add_argument('-s', '--size', type=int)
parser.add_argument('-m', '--mode', type=str, help='choose WHITE or BLACK')
parser.add_argument('-t', '--threshold', type=int,
help='threshold between white and black')
parser.add_argument('-c', '--scale', type=int, default=1)
args = parser.parse_args()
N = 16
if args.size:
N = args.size
if args.threshold:
THRESHOLD = args.threshold
N_WIDTH = N//2
N_HEIGHT = N
in_file = None
if args.input is None:
in_file = sys.stdin.buffer
else:
in_file = open(args.input, 'rb')
out_file = None
if args.output is None:
out_file = sys.stdout.buffer
else:
out_file = open(args.output, 'wb')
frame = loadFrame(in_file.read())
in_file.close()
opts = {
'threshold': 90,
'chars': CHARS,
'palette': np.arange(len(CHARS)),
'n_height': N_HEIGHT,
'n_width': N_WIDTH, 'mode': args.mode}
text = frameToText(frame, **opts)
text_split = text.split('\n')
height = len(text_split)-1
width = len(text_split[1])
frame = textToFrame(
text, FONT, *(v * args.scale for v in frame.shape[0:2]), height, width)
result, encoded_frame = cv2.imencode('.png', frame)
if not result:
print('could not encode image', file=sys.stderr)
exit(1)
out_file.write(bytearray(encoded_frame))
out_file.close()