export_animation task
parent
8fb715c051
commit
2eee1624f4
@ -0,0 +1,68 @@
|
|||||||
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
from call.models import Creature
|
||||||
|
import weasyprint as wsp
|
||||||
|
import subprocess
|
||||||
|
from django.urls import reverse
|
||||||
|
from urllib.parse import urljoin, urlencode
|
||||||
|
from PIL import Image
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = "Generates an animation"
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument("--baseurl", default="http://localhost:8000/")
|
||||||
|
parser.add_argument("--output", default="animation_{width}x{height}.gif")
|
||||||
|
parser.add_argument("--width", type=int, default=500)
|
||||||
|
parser.add_argument("--height", type=int, default=500)
|
||||||
|
parser.add_argument("--scale", type=int, default=6)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
baseurl = options['baseurl']
|
||||||
|
width = options['width']
|
||||||
|
height = options['height']
|
||||||
|
scale = options['scale']
|
||||||
|
output_filename = options['output'].format(**{'width': width, 'height': height})
|
||||||
|
# url = "http://localhost:8000/creatures/39/"
|
||||||
|
# w = wsp.HTML(url=url)
|
||||||
|
# w.write_pdf("test.pdf")
|
||||||
|
#
|
||||||
|
# frames = []
|
||||||
|
frameno = 0
|
||||||
|
convert_args = ["convert", "-delay", "25"]
|
||||||
|
qvars = {'width': width, 'height': height, 'scale': scale}
|
||||||
|
for creature in Creature.objects.filter(published=True).order_by("-created"):
|
||||||
|
url = urljoin(baseurl, reverse("creature_detail", args=[creature.id,]))
|
||||||
|
print (creature, url)
|
||||||
|
qvars['cell'] = 0
|
||||||
|
w = wsp.HTML(url=f"{url}?{urlencode(qvars)}")
|
||||||
|
w.write_pdf("temp.pdf")
|
||||||
|
subprocess.run(["convert", "-density", "96", "temp.pdf", "temp.png"])
|
||||||
|
# frames.append(Image.open("temp.png"))
|
||||||
|
fn1 = f"frame{frameno:04}.png"
|
||||||
|
os.rename("temp.png", fn1)
|
||||||
|
frameno += 1
|
||||||
|
convert_args.append(fn1)
|
||||||
|
|
||||||
|
qvars['cell'] = 3
|
||||||
|
w = wsp.HTML(url=f"{url}?{urlencode(qvars)}")
|
||||||
|
w.write_pdf("temp.pdf")
|
||||||
|
subprocess.run(["convert", "-density", "96", "temp.pdf", "temp.png"])
|
||||||
|
# frames.append(Image.open("temp.png"))
|
||||||
|
fn2 = f"frame{frameno:04}.png"
|
||||||
|
os.rename("temp.png", fn2)
|
||||||
|
frameno += 1
|
||||||
|
convert_args.append(fn2)
|
||||||
|
for i in range(7):
|
||||||
|
convert_args.append(fn1)
|
||||||
|
convert_args.append(fn2)
|
||||||
|
|
||||||
|
# convert -delay 100 "frame????.png" "animation.imc.gif"
|
||||||
|
convert_args.append(output_filename)
|
||||||
|
subprocess.run(convert_args)
|
||||||
|
subprocess.run("rm temp.pdf frame*.png", shell=True)
|
||||||
|
|
||||||
|
# # self.stdout.write(
|
||||||
|
# # self.style.SUCCESS('Successfully closed poll "%s"' % poll_id)
|
||||||
|
# # )
|
Loading…
Reference in New Issue