-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext2qrcode.py
40 lines (37 loc) · 1.08 KB
/
text2qrcode.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
# -*- coding: utf-8 -*
import qrcode, zlib
import os, sys, getopt
line = 100
if len(sys.argv) < 2:
raise Exception("Please input one file name as a object!")
name = sys.argv[1]
if not os.path.exists(name):
raise Exception("The file (%s) does not exist!" % name)
suffix = ""
compress = False
opts, args = getopt.getopt(sys.argv[2:],"",["compress","line="])
for op, value in opts:
if op == "--compress":
compress = True
elif op == "--line":
line = int(value)
with open("%s"%name) as f:
t = f.readlines()
l = len(t)//line+1
for i in range(l):
s = ''.join(t[i*line:(i+1)*line])
z = zlib.compress(s.encode())
print(len(s), "before compress")
print(len(z), "after compress")
if compress:
s = z
suffix = "_compress"
print("compress")
try:
img = qrcode.make(s)
except Exception as e:
raise Exception("Maybe the file is too long. Try to add command option --line 30.")
break
imgname = '%s.png'%(name.split('.')[0]+'_'+str(i+1)+suffix)
img.save(imgname)
print("Finish! See %s!"%imgname)