-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
70 lines (51 loc) · 2.82 KB
/
script.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
import os, argparse, sys
from PIL import Image
def main():
parser = argparse.ArgumentParser()
parser.add_argument("input",type=str,help="image file")
parser.add_argument("-o","--output",type=str,help="specify output directory")
parser.add_argument("-n","--name",type=str,help="name of output file (default name is 'output.html')")
parser.add_argument("-t","--type",type=str,choices=["code","website"],help="'website' puts out the file with head and body tags while 'code' only saves the table part in a file")
parser.add_argument("-b","--border",action='store_true',help="turns on border")
parser.add_argument("--nocollapse",action='store_true',help="disables border collapse")
parser.add_argument("-v","--overwrite",action='store_true',help="can replace existing output file")
parser.add_argument("-r","--resize",type=int,help="resizes image. value is new width in pixel. --> changes amounnt of pixels")
parser.add_argument("-s","--size",type=int,help="width of the resulting table in pixel --> changes size of table")
args = parser.parse_args()
in_file = args.input
out_path = args.output if not args.output == None else os.getcwd()
do_overwrite = args.overwrite
out_name = args.name if not args.name == None else "output.html"
is_full_html = True if args.type == "code" else False
out_file = os.path.join(out_path,out_name)
style_border = "border: 1px solid black;" if args.border else ""
style_collapse = "border-collapse: none;" if args.nocollapse else "border-collapse: collapse;"
im = Image.open(in_file)
if not args.resize == None:
size = (args.resize,args.resize*im.height//im.width)
im = im.resize(size)
if not do_overwrite and os.path.isfile(out_file):
print("File exists. Terminating script. Add '--overwrite' / '-v' or change the outputs name with '--name' / '-n'")
sys.exit()
f = open(out_file,"w")
if is_full_html:
f.write("<head></head><body>")
style = style_border+style_collapse
table_width = im.size[0] if args.size == None else args.size
table_height = im.size[1] if args.size == None else im.size[1]*args.size//im.size[0]
f.write('<table style="'+style+'width:'+str(table_width)+'px;height:'+str(table_height)+'px;">')
px = im.load()
cell_height = "%.3f" % ((1/table_height)*100)
cell_width = "%.3f" % ((1/table_width)*100)
for y in range(im.size[1]):
f.write('<tr style="'+style+'">')
for x in range(im.size[0]):
f.write('<td style="'+style+';width:'+cell_width+'%;height:'+cell_height+'%;background-color:rgb'+str(px[x,y])+'"></td>')
f.write("</tr>")
f.write("</table>")
if is_full_html:
f.write("</body>")
f.close()
print("Done. ("+out_file+")")
if __name__ == "__main__":
main()