-
Notifications
You must be signed in to change notification settings - Fork 1
/
biqu_convert_p24.py
167 lines (131 loc) · 4.05 KB
/
biqu_convert_p24.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/local/bin/python3
import base64
import io, os, shutil
import sys, getopt
try:
from PyQt5.QtCore import QSize,Qt
from PyQt5.QtCore import QFile, QFileInfo, QIODevice,QTextStream
from PyQt5.QtGui import QImage
except:
print ("!!! PyQt5 not installed run pip install PyQt5 !!!")
sys.exit()
CODEC = "UTF-8"
def i4b(n):
return [n >> 24 & 0xFF,n >> 16 & 0xFF,n >> 8 & 0xFF,n >> 0 & 0xFF]
def i2b(n):
return [n >> 8 & 0xFF,n >> 0 & 0xFF]
def overread(msize,gfile):
moutdata = ""
img = QImage()
img.loadFromData(base64topng(readPSTump(gfile),''))
img = img.scaled(msize.width(),msize.height(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
moutdata += ";"+(hex(msize.width())[2:]).rjust(4,'0')+(hex(msize.height())[2:]).rjust(4,'0')+"\r\n"
pos = QSize(0,0)
for ypos in range(0,img.height()):
qrgb =";"
for xpos in range(0,img.width()):
data = img.pixel(xpos,ypos)
pos.setWidth(pos.width()+1)
dummy = (hex(((data & 0x00F80000) >> 8 ) | ((data & 0x0000FC00) >> 5 ) | ((data & 0x000000F8) >> 3 ))[2:]).rjust(4,'0')
if dummy == "0020" or dummy == "0841" or dummy == "0861":
dummy = "0000"
qrgb = qrgb + dummy
pos.setWidth(0)
pos.setHeight(pos.height()+1)
moutdata = moutdata + qrgb + "\r\n"
return moutdata
def overseek(gfile):
outdatar = ""
outdatar = outdatar + overread(QSize(70,70),gfile)
outdatar = outdatar + overread(QSize(95,80),gfile)
outdatar = outdatar + overread(QSize(95,95),gfile)
outdatar = outdatar + overread(QSize(160,140),gfile)
return outdatar
def do_convert(gfile):
env_slicer_pp_output_name = str(os.getenv('SLIC3R_PP_OUTPUT_NAME'))
final_name = os.path.splitext(env_slicer_pp_output_name)[0]+"_btt"+os.path.splitext(env_slicer_pp_output_name)[1]
with open(gfile + '.output_name', mode='w', encoding='UTF-8') as fopen:
fopen.write(final_name)
outdata = ""
outdata = overseek(gfile)
outdata = outdata + "; bigtree thumbnail end\r\n\r\n"
fh = QFile(gfile)
fh.open(QIODevice.ReadOnly)
stream = QTextStream(fh)
stream.setCodec(CODEC)
lino = 0
fg = stream.readAll()
fh.close()
bigtree3dfile = gfile
fh = QFile(bigtree3dfile)
fh.open(QIODevice.WriteOnly)
stream = QTextStream(fh)
stream.setCodec(CODEC)
stream << outdata
stream << fg
fh.close()
## write base64 to png
def base64topng(stringdata,imagepath):
image = base64.b64decode(stringdata)
return image
def readPSTump(infile):
count = 0
base64data =''
started = 0
ended = 0
Psfile = open(infile,"r")
while True:
count =count+1
# Get next line from file
line = Psfile.readline()
if line.find("; thumbnail begin") == 0:
started = 1
continue
if line.find("; thumbnail end") == 0:
ended = 1
if ended == 1:
break
# if line is empty
# end of file is reached
if not line:
break
if started == 0:
continue
base64data += line[2:]
Psfile.close()
if base64data == '':
print('!!! no thumbnail data found in gcode !!!')
sys.exit()
##with open('base64.txt', 'w') as fh:
## fh.writelines("%s" % line for line in base64data)
return base64data
def readPSGcode(infile):
gcodedata =''
started = 0
Psfile = open(infile,"r")
while True:
# Get next line from file
line = Psfile.readline()
if line.find("; thumbnail end") == 0:
started = 1
continue
if started == 0:
continue
# if line is empty
# end of file is reached
if not line:
break
gcodedata += line
Psfile.close()
return gcodedata
def main(argv):
inputfile = ''
outputfile = ''
try:
inputfile = sys.argv[1]
except:
print('./biqu_convert inputfile')
sys.exit()
do_convert(inputfile)
if __name__ == "__main__":
main(sys.argv[1:])