diff --git a/packages/preview_generator/README.md b/packages/preview_generator/README.md new file mode 100644 index 0000000..aad374e --- /dev/null +++ b/packages/preview_generator/README.md @@ -0,0 +1,7 @@ +WoopWOopWoop + +![Preview](preview_1046037910.gif) + +```layers +static() top #FF0000 +``` \ No newline at end of file diff --git a/packages/preview_generator/app.py b/packages/preview_generator/app.py index 407daef..23baaae 100644 --- a/packages/preview_generator/app.py +++ b/packages/preview_generator/app.py @@ -1,39 +1,6 @@ import argparse import sys -from klippermock import * -from PIL import Image, ImageDraw - -def run(layers, fps, seconds, leds, diameter, margin, output): - config = mockConfig() - config.setint("ledcount", leds) - config.setint("framerate", fps) - config.set("layers", layers) - printer = mockPrinter(config) - printer._handle_ready() - - width = leds*diameter + margin*(leds+1) - height = diameter+margin*2 - progressThickness = 3 - frames = [] - - for i in range(seconds*fps): - frame = printer.led_effect.getFrame(i)[0] - img = Image.new('RGBA', (width, height), color=(0, 0, 0, 0)) - draw = ImageDraw.Draw(img) - draw.line([(0, height-progressThickness), (width*i/(seconds*fps), height-progressThickness)], fill="black", width=progressThickness) - for j in range(0, len(frame), 4): - led = j/4 - x0 = margin + led*(diameter+margin) - y0 = margin - x1 = x0 + diameter - y1 = y0 + diameter - - [r, g, b, a] = frame[j:j+4] - draw.ellipse([(x0, y0), (x1, y1)], width=2, outline="black", fill=(int(r*255), int(g*255), int(b*255), 255)) - frames.append(img) - - frames[0].save(output, save_all=True, append_images=frames[1:], duration=1000/fps, loop=0) - +from preview_generator import generate_preview if __name__ == '__main__': @@ -51,5 +18,5 @@ def run(layers, fps, seconds, leds, diameter, margin, output): layers = args.layers if args.layers != sys.stdin else sys.stdin.read() - run(layers, args.fps, args.seconds, args.leds, args.diameter, args.margin, args.output) + generate_preview(layers, args.fps, args.seconds, args.leds, args.diameter, args.margin, args.output) diff --git a/packages/preview_generator/markdown_generator.py b/packages/preview_generator/markdown_generator.py new file mode 100644 index 0000000..2bdcb38 --- /dev/null +++ b/packages/preview_generator/markdown_generator.py @@ -0,0 +1,45 @@ +import re +import zlib +import os +import argparse +from preview_generator import generate_preview + +args = None + +def replace_with_preview(match): + layers = match.group(2) + filename = "preview_" + str(zlib.crc32(layers.encode('utf-8')))+".gif" + # If file doesn't exist + if not os.path.exists(filename): + print("Generating "+filename) + generate_preview(layers, args.fps, args.seconds, args.leds, args.diameter, args.margin, filename) + else: + print("Using existing "+filename) + return """![Preview](%s) + +```layers +%s +```""" % (filename, layers) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + prog='Preview-Generator', + description='Generates an animated image of a given layer') + parser.add_argument('markdown', type=argparse.FileType('r')) + parser.add_argument('--fps', default=24, type=int) + parser.add_argument('--seconds', '-s', default=10, type=int) + parser.add_argument('--diameter', '-d', default=50, type=int) + parser.add_argument('--margin', '-m', default=5, type=int) + parser.add_argument('--leds', '-l', default=10, type=int) + args = parser.parse_args() + + markdown = args.markdown.read() + args.markdown.close() + markdown = re.sub(r'(!\[Preview\]\(.+\)\n+)?```layers\n(.*?)\n```', replace_with_preview, markdown) + file = open(args.markdown.name, 'w') + file.write(markdown) + file.close() + + + \ No newline at end of file diff --git a/packages/preview_generator/preview_generator/__init__.py b/packages/preview_generator/preview_generator/__init__.py new file mode 100644 index 0000000..850a1ee --- /dev/null +++ b/packages/preview_generator/preview_generator/__init__.py @@ -0,0 +1 @@ +from .preview_generator import generate_preview \ No newline at end of file diff --git a/packages/preview_generator/preview_generator/preview_generator.py b/packages/preview_generator/preview_generator/preview_generator.py new file mode 100644 index 0000000..1b4eede --- /dev/null +++ b/packages/preview_generator/preview_generator/preview_generator.py @@ -0,0 +1,33 @@ +from klippermock import * +from PIL import Image, ImageDraw + +def generate_preview(layers, fps, seconds, leds, diameter, margin, output): + config = mockConfig() + config.setint("ledcount", leds) + config.setint("framerate", fps) + config.set("layers", layers) + printer = mockPrinter(config) + printer._handle_ready() + + width = leds*diameter + margin*(leds+1) + height = diameter+margin*2 + progressThickness = 3 + frames = [] + + for i in range(seconds*fps): + frame = printer.led_effect.getFrame(i)[0] + img = Image.new('RGBA', (width, height), color=(0, 0, 0, 0)) + draw = ImageDraw.Draw(img) + draw.line([(0, height-progressThickness), (width*i/(seconds*fps), height-progressThickness)], fill="black", width=progressThickness) + for j in range(0, len(frame), 4): + led = j/4 + x0 = margin + led*(diameter+margin) + y0 = margin + x1 = x0 + diameter + y1 = y0 + diameter + + [r, g, b, a] = frame[j:j+4] + draw.ellipse([(x0, y0), (x1, y1)], width=2, outline="black", fill=(int(r*255), int(g*255), int(b*255), 255)) + frames.append(img) + + frames[0].save(output, save_all=True, append_images=frames[1:], duration=1000/fps, loop=0)