forked from julianschill/klipper-led_effect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(preview_generator): Process markdown files
- Loading branch information
Showing
5 changed files
with
88 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
WoopWOopWoop | ||
|
||
![Preview](preview_1046037910.gif) | ||
|
||
```layers | ||
static() top #FF0000 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .preview_generator import generate_preview |
33 changes: 33 additions & 0 deletions
33
packages/preview_generator/preview_generator/preview_generator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |