Skip to content

Commit

Permalink
Use in-memory buffer for canvas image instead of temporary file
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBruens authored and Harvie committed Jun 25, 2024
1 parent 44a03cf commit 0bc9dcc
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions bCNC/Pendant.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import tempfile
import threading
import io

import Camera
from CNC import CNC
Expand Down Expand Up @@ -140,28 +141,22 @@ def do_GET(self):
elif page == "/canvas":
if not Image:
return
with tempfile.NamedTemporaryFile(suffix=".ps") as tmp:
httpd.app.canvas.postscript(
file=tmp.name,
colormode="color",
)
tmp.flush()
ps = httpd.app.canvas.postscript(colormode="color")
try:
with io.BytesIO() as out:
Image.open(io.BytesIO(ps.encode('utf-8'))).save(out, "gif")
self.do_HEAD(200, content="image/gif", cl=out.tell())
out.seek(0)
self.wfile.write(out.read())
except Exception:
filename = os.path.join(iconpath, "warn.gif")
try:
with tempfile.NamedTemporaryFile(suffix=".gif") as out:
Image.open(tmp.name).save(out.name, "GIF")
out.flush()
self.do_HEAD(200, content="image/gif", cl=out.tell())
out.seek(0)
self.wfile.write(out.read())
f = open(filename,"rb")
self.do_HEAD(200, content="image/gif", cl=self.get_file_size(f))
self.wfile.write(f.read())
f.close()
except Exception:
filename = os.path.join(iconpath, "warn.gif")
try:
f = open(filename,"rb")
self.do_HEAD(200, content="image/gif", cl=self.get_file_size(f))
self.wfile.write(f.read())
f.close()
except Exception:
pass
pass

elif page == "/camera":
if not Camera.hasOpenCV():
Expand Down

0 comments on commit 0bc9dcc

Please sign in to comment.