You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.2 KiB

from tkinter import *
from tkinter import filedialog
6 years ago
from tkinter.ttk import Notebook
6 years ago
from PIL import Image, ImageTk
import os
6 years ago
from GCodeRenderer import Renderer
6 years ago
from Svg2GcodeConverter import Svg2GcodeConverter, triangulate_lengths, untriangulate_lengths
from ImageConverter import ImageConverter
from Simulator import Simulator
6 years ago
class Settings:
6 years ago
def __init__(self):
# ============ HARDCODED VALUES ===========
# Canvas size
4 years ago
self.canvas_x = 200
self.canvas_y = 200
# The position of the pulley centers in relation to the top left and right of the canvas
self.left_pulley_x_offset = -40
self.right_pulley_x_offset = 40
self.pulley_y_droop = 60
# Diameter of the inner portion of the pulley in millimeters
self.pulley_diameter = 45
# Feed rates
6 years ago
self.speed = 1000
6 years ago
# Whether we render lift markers
self.lift_markers = False
self.lift_counter = 0
# ============ CALCULATED VALUES ===========
6 years ago
self.distance_between_centers = abs(self.left_pulley_x_offset) + self.canvas_x + self.right_pulley_x_offset
6 years ago
# Main GUI class and program entry point
6 years ago
class Tracer(Tk):
6 years ago
def update_highpass_value(self, value):
self.highpass_filter = value
6 years ago
def update_blur_value(self, value):
self.blur = value
6 years ago
def update_turd_value(self, value):
self.turd = value
6 years ago
def __init__(self):
super().__init__()
# Setup the file structure
if not os.path.exists("output"):
os.makedirs("output")
if not os.path.exists("tmp"):
os.makedirs("tmp")
# Settings for the printer are loaded, TODO: Customize for our dual motor printer
6 years ago
self.settings = Settings()
# Image filename which we are converting
6 years ago
self.filename = None
6 years ago
# GCODE -> SVG,PNG renderer
6 years ago
self.cairo_renderer = Renderer(self.settings)
# SVG -> GCODE converter
6 years ago
self.gcode_converter = Svg2GcodeConverter(self.settings)
6 years ago
# FILE -> SVG converter
self.image_converter = ImageConverter()
self.image_converter_settings = ImageConverter.ConverterSettings()
6 years ago
self.label = None
self.pix = None
self.label1 = None
self.image_ref = None
# Initialize TK
self.geometry("{}x{}".format(800, 800))
6 years ago
self.tab_bar = Notebook(self, width= 400, height =400)
self.tab_bar.pack(fill=BOTH, expand=1)
6 years ago
self.converted_image_tab = Frame(self.tab_bar)
self.original_image_tab = Frame(self.tab_bar)
6 years ago
self.rightframe = Frame(self)
self.rightframe.pack(side=RIGHT)
self.centerframe = Frame(self)
self.centerframe.pack(side=BOTTOM)
self.image_select_button = Button(self.rightframe, text="Select Image", command=self.file_select_callback)
self.image_select_button.pack()
6 years ago
self.rerender_button = Button(self.rightframe, text="Re-Render", command=self.render)
self.rerender_button.pack()
6 years ago
self.render_simulation_button = Button(self.rightframe, text="Render Simulation", command=self.render_simulation)
self.render_simulation_button.pack()
6 years ago
self.lift_markers_checkbox = Checkbutton(self.rightframe, text="Lift Markers", command=self.cairo_renderer.toggle_flip_markers)
self.lift_markers_checkbox.pack()
self.highpass_label = Label(self.centerframe, text="Highpass filter", fg="black")
self.highpass_label.pack()
self.highpass_slider = Scale(self.centerframe, command=self.update_highpass_value, resolution=0.0, to=15, orient=HORIZONTAL)
self.highpass_slider.set(self.image_converter_settings.highpass_filter)
6 years ago
self.highpass_slider.pack()
self.blur_label = Label(self.centerframe, text="Blur", fg="black")
self.blur_label.pack()
self.blur_slider = Scale(self.centerframe, command=self.update_blur_value, resolution=0.0, to=5, orient=HORIZONTAL)
self.blur_slider.set(self.image_converter_settings.blur)
6 years ago
self.blur_slider.pack()
self.turd_label = Label(self.centerframe, text="Turds", fg="black")
self.turd_label.pack()
self.turd_slider = Scale(self.centerframe, command=self.update_turd_value, resolution=0.0, to=5, orient=HORIZONTAL)
self.turd_slider.set(self.image_converter_settings.turd)
self.turd_slider.pack()
6 years ago
# Start TK
self.mainloop()
def file_select_callback(self):
6 years ago
filepath = filedialog.askopenfilename(initialdir=".", title="Select file",
filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))
# User didn't select a file
if len(filepath) is 0:
return
self.update_idletasks()
6 years ago
self.filename = os.path.basename(filepath)
self.render()
6 years ago
6 years ago
def render(self):
self.image_converter.convert_image(self.filename, self.image_converter_settings)
6 years ago
self.gcode_converter.convert_gcode()
self.cairo_renderer.clear_screen()
self.cairo_renderer.render_gcode()
self.converted_image_tab.pack_forget()
self.original_image_tab.pack_forget()
6 years ago
if self.label is not None:
self.label.pack_forget()
if self.label1 is not None:
self.label1.pack_forget()
6 years ago
pil_image = Image.open("tmp/rendered-output.png")
# scale = self.winfo_width() / pil_image.width
# pil_image = pil_image.resize((int(scale * pil_image.width), int(scale * pil_image.height)))
6 years ago
self.image_ref = ImageTk.PhotoImage(pil_image)
self.label = Label(self.converted_image_tab, image=self.image_ref)
self.tab_bar.add(self.converted_image_tab, text="Converted")
6 years ago
self.label.pack(expand=True, fill="both")
6 years ago
self.pic = ImageTk.PhotoImage(file="input-images/{}".format(self.filename))
6 years ago
self.label1 = Label(self.original_image_tab, image=self.pic)
self.tab_bar.add(self.original_image_tab, text="Original")
6 years ago
self.label1.pack(expand=True, fill="both")
def render_simulation(self):
6 years ago
simulator = Simulator()
simulator.render()
settings = Settings()
print(triangulate_lengths(settings, (150, 0)))
6 years ago
# print(triangulate_lengths(settings, (300, 300)))
6 years ago
if __name__ == "__main__":
6 years ago
Tracer()
6 years ago