Commit d7981276 authored by gijs's avatar gijs

Animation generation scripts

parent a957026e
# Transform SVG to PNG
for FILE in svg/*.svg
do
FILENAME=$(basename $FILE)
inkscape -z -e png/${FILENAME%.*}.png -w 1080 -h 1920 $FILE;
done
ffmpeg -i png/%05d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p poster.mp4;
\ No newline at end of file
# Transform SVG to PNG
for FILE in svg/*.svg
do
FILENAME=$(basename $FILE)
inkscape -z -e png/${FILENAME%.*}.png -w 1920 -h 1080 $FILE;
done
ffmpeg -i png/%05d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p poster.mp4;
\ No newline at end of file
# Transform SVG to PNG
for FILE in svg/*.svg
do
FILENAME=$(basename $FILE)
inkscape -z -e png/${FILENAME%.*}.png -w 1920 -h 2160 $FILE;
done
ffmpeg -i png/%05d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p poster.mp4;
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
from svg.path import parse_path
from svg.path.path import Move, Line, CubicBezier
from bs4 import BeautifulSoup
import re
# height = 800
# width = height/(1920/1080)
def get_group_by_label (svg, label):
for group in svg.find_all('g'):
if group.get('inkscape:label') == label:
return group
def get_keypoints(svg):
return [complex(float(point.get('cx')), float(point.get('cy'))) for point in get_group_by_label(svg, 'keypoints').find_all('circle')]
with open('timeline.inkscape.svg', 'r') as handle:
# use bs 4 to find paths
soup = BeautifulSoup(handle.read(), 'lxml')
svg = soup.find('svg')
# path = soup.find('path')
# segments = parse_path(path.get('d'))
# svg['width'] = width
# svg['height'] = height
# svg['viewbox'] = '0 0 {} {}'.format(width, height)
# layers = [
# {
# 'keyframes': [
# (0, (-200,0))
# (30, (-200,0))
# ]
# }
# ]
# lines = []
keypoints_layer = get_group_by_label(soup, 'keypoints')
keypoints = sorted(get_keypoints(soup), key=lambda point: point.real)
# for segment in segments:
# if isinstance(segment, Line):
# lines.append(segment)
# for i in range(0,len(lines)-1):
# # print(lines[i].start.real)
# center = lines[i].end + (lines[i+1].start - lines[i].end) / 2
# # print(center.real)
# print(center)
# loopcenters.append(center)
pattern = re.compile('translate\(([\d\.]+),\s*([\d\.]+)\)')
animation = get_group_by_label(soup, 'animation')
transform = animation.get('transform')
if transform:
m = pattern.match(transform)
if m:
translation = (float(m.group(1)), float(m.group(2)))
else:
translation = (0, 0)
else:
translation = (0, 0)
keypoints_layer.extract()
for i, center in enumerate(keypoints):
animation['transform'] = 'translate({}, {})'.format(translation[0] + (-1 * (center.real - (float(svg['width']) / 2))), translation[1])
# print(path, 'translate({}, {})'.format(center.real - (width / 2), 0))
with open('frames/{}.svg'.format(i), 'w') as o:
o.write(svg.prettify(formatter=None)
.replace('lineargradient', 'linearGradient')
.replace('gradienttransform', 'gradientTransform')
.replace('gradientunits', 'gradientUnits')
.replace('spreadmethod', 'spreadMethod'))
o.close()
# output = svgwrite.Drawing(filename="frames/output.svg", size=(width, height))
\ No newline at end of file
from svg.path import parse_path
from svg.path.path import Move, Line, CubicBezier
from bs4 import BeautifulSoup
import re
import os.path
# height = 800
# width = height/(1920/1080)
sources = [
# ('digital-city-poster-1080x1920.svg', '1080x1920'),
('digital-city-poster-1920x1080.svg', '1920x1080'),
('digital-city-poster-1920x2160.svg', '1920x2160')
]
fps = 25
still = 3
transition = .5
def parse_viewbox (viewboxstring):
pattern = re.compile(r'(-?[\d\.]+) (-?[\d\.]+) (-?[\d\.]+) (-?[\d\.]+)')
m = pattern.match(viewboxstring)
return (float(m.group(1)), float(m.group(2)), float(m.group(3)), float(m.group(4)))
def framecount (length):
return int(length * fps)
def get_translation (group):
pattern = re.compile(r'translate\((-?[\d\.]+),\s*(-?[\d\.]+)\)')
transform = group.get('transform')
if transform:
m = pattern.match(transform)
if m:
translation = (float(m.group(1)), float(m.group(2)))
else:
translation = (0, 0)
else:
translation = (0, 0)
return translation
def get_group_by_label (svg, label):
for group in svg.find_all('g'):
if group.get('inkscape:label') == label:
return group
def get_keypoints(svg):
keypoints_layer = get_group_by_label(svg, 'keypoints')
keypoints = keypoints_layer.find_all('circle')
translation = get_translation(keypoints_layer)
return [
complex(translation[0] + float(point.get('cx')), translation[1] + float(point.get('cy'))) for point in keypoints]
def write_frame(f, svg, folder = ''):
with open(os.path.join(folder, '{:05d}.svg'.format(f)), 'w') as o:
o.write(svg.prettify(formatter=None)
.replace('lineargradient', 'linearGradient')
.replace('gradienttransform', 'gradientTransform')
.replace('gradientunits', 'gradientUnits')
.replace('spreadmethod', 'spreadMethod')
.replace('viewbox', 'viewBox')
.replace('flowroot', 'flowRoot')
.replace('textpath', 'textPath')
.replace('spreadmethod', 'spreadMethod')
.replace('clippath', 'clipPath'))
o.close()
for drawing, target in sources:
print(drawing)
with open(drawing, 'r') as handle:
# use bs 4 to find paths
soup = BeautifulSoup(handle.read(), 'lxml')
svg = soup.find('svg')
viewbox = parse_viewbox(svg['viewbox'])
print(viewbox)
width = viewbox[2] - viewbox[0]
keypoints_layer = get_group_by_label(soup, 'keypoints')
keypoints = sorted(get_keypoints(soup), key=lambda point: point.real)
animation = get_group_by_label(soup, 'animation')
translation = get_translation(animation)
keypoints_layer.extract()
f = 0
for k, center in enumerate(keypoints):
if k > 0:
for i in range(framecount(transition)):
t = i / float(framecount(transition))
d = keypoints[k].real - keypoints[k-1].real
x = translation[0] - keypoints[k-1].real - (t * d) + (float(width) / 2)
y = translation[1]
animation['transform'] = 'translate({}, {})'.format(x, y)
get_group_by_label(svg, 'text')['visibility'] = 'hidden'
write_frame(f, svg, folder=os.path.join(target, 'svg'))
f += 1
for i in range(framecount(still)):
x = translation[0] + (-1 * (center.real - (float(width) / 2)))
y = translation[1]
animation['transform'] = 'translate({}, {})'.format(x, y)
get_group_by_label(svg, 'text')['visibility'] = 'visible'
write_frame(f, svg, folder=os.path.join(target, 'svg'))
f += 1
# print(path, 'translate({}, {})'.format(center.real - (width / 2), 0))
# output = svgwrite.Drawing(filename="frames/output.svg", size=(width, height))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment