-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgcode-with-thumbnail-thumbnailer.py
More file actions
95 lines (79 loc) · 3.14 KB
/
gcode-with-thumbnail-thumbnailer.py
File metadata and controls
95 lines (79 loc) · 3.14 KB
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
#!/usr/bin/env python3
#
# gcode-with-thumbnail-thumbnailer.py - A Gnome 3 thumbnailer for gcode with an embedded thumbnail
#
# Copyright (C) 2022 Peter Ellens <ellensp@hotmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/gpl>.
# # Contains code from the jpg re-encoder thumbnail post processor script:
# https://raw.githubusercontent.com/alexqzd/Marlin/Gcode-preview/Slicer%20post-processing%20scripts/PrusaSlicer_JPEG_Preview.py
# and https://github.com/MestreLion/icns-thumbnailer
import sys
import re
import base64
import io
import subprocess
try:
from PIL import Image
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "Pillow"])
from PIL import Image
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
def make_thumbnail(inputname, outputname, size=0):
try:
sourceFile = sys.argv[1]
# Read the ENTIRE g-code file into memory
with open(sourceFile, "r") as f:
lines = f.read()
f.close()
thumb_expresion = '; thumbnail begin.*?\n((.|\n)*?); thumbnail end'
thumb_matches = re.findall(thumb_expresion, lines)
if not thumb_matches:
sys.stderr.write("Thumbnail data not found\n")
return 1
for idx, match in enumerate(thumb_matches):
original = match[0]
encoded = original.replace("; ", "")
encoded = encoded.replace("\n", "")
encoded = encoded.replace("\r", "")
decoded = base64.b64decode(encoded)
pixbuf = Image.open(io.BytesIO(decoded))
if size:
width, height = pixbuf.size
if width > height:
if width > size:
height = height * size / width
width = size
else:
if height > size:
width = width * size / height
height = size
scaled = pixbuf.resize((int(width),int(height)), Image.LANCZOS)
else:
scaled = pixbuf
scaled.save(outputname,'png')
except GLib.GError as e:
sys.stderr.write("%s:%d: %s\n" % (e.domain, e.code, e))
return e.code
def main(argv):
try:
args = argv[1], argv[2], int((argv[3:4] or [0])[0])
if len(argv) > 4: raise IndexError
except (ValueError, IndexError):
sys.stderr.write("Usage: %s inputfile outputfile [size]\n" % argv[0])
return 1
return make_thumbnail(*args)
if __name__ == '__main__':
sys.exit(main(sys.argv))