Skip to content

Commit

Permalink
feature(preview_generator): Process markdown files
Browse files Browse the repository at this point in the history
  • Loading branch information
reemo3dp committed Mar 11, 2024
1 parent 536eb5b commit 8b88a91
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 35 deletions.
7 changes: 7 additions & 0 deletions packages/preview_generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
WoopWOopWoop

![Preview](preview_1046037910.gif)

```layers
static() top #FF0000
```
37 changes: 2 additions & 35 deletions packages/preview_generator/app.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand All @@ -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)

45 changes: 45 additions & 0 deletions packages/preview_generator/markdown_generator.py
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()



1 change: 1 addition & 0 deletions packages/preview_generator/preview_generator/__init__.py
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 packages/preview_generator/preview_generator/preview_generator.py
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)

0 comments on commit 8b88a91

Please sign in to comment.