Skip to content

Instantly share code, notes, and snippets.

@benjamin-kirkbride
Created October 12, 2018 23:23
Show Gist options
  • Save benjamin-kirkbride/006e63fef1152418ae4ac1ea4f6b4048 to your computer and use it in GitHub Desktop.
Save benjamin-kirkbride/006e63fef1152418ae4ac1ea4f6b4048 to your computer and use it in GitHub Desktop.
batik export
import subprocess
import tempfile
import time
import shlex
from pathlib import Path
class Error(Exception):
""" Base Error Class for batik"""
pass
class JavaNotFound(Error):
""" Java not found on the system"""
pass
class ExportFailedError(Error):
""" Batik Export Failed"""
pass
class Export:
""" Wrapper for creating and managing an batik instance"""
def __init__(self, inputs, width, height, output_dir, delete_input=False):
self.inputs = inputs
self.stdout = tempfile.NamedTemporaryFile()
self.stderr = tempfile.NamedTemporaryFile()
bakit = str(Path('./bin/batik-1.9/batik-rasterizer-1.9.jar'))
command = [
java, '-jar', bakit, ' '.join(self.inputs), '-d', output_dir,
'-w', str(width), '-h', str(height)]
self.batik = subprocess.Popen(command)
self.start_time = time.time()
def is_done(self):
""" Returns True if batik is done exporting
False means is processing data"""
# checks to ensure that process hasn't stopped
status = self.batik.poll()
if status == None:
return False
elif status == 0:
return True
else:
raise ExportFailedError
def get_java():
# platform dependent
java_paths = [
'java',
'java.exe',
'C:\Program Files\Java\jdk1.7.0\\bin\javac',
'/usr/bin/java',
'C:\Program Files\Java\jdk1.6.0_30\\bin\javac.exe',
]
# import test code
for path in java_paths:
try:
subprocess.run(
path, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return path
break
except FileNotFoundError:
continue
else:
raise JavaNotFound
java = get_java()
import sys
import os
import time
sys.path.append(os.path.join(os.getcwd(), 'lib'))
from batik import *
inputs = []
for i in range(100):
inputs.append('./resources/template/lower_third_class.svg')
export = Export(inputs, 1920, 1080, './output')
while True:
if export.is_done():
break
time.sleep(0.05)
About to transcode 1 SVG file(s)
Converting file:./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg ./resources/template/lower_third_class.svg to ./output/lower_third_class.png ... ... error (SVGConverter.error.cannot.open.source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment