Simplify card sheet generation by keeping files as SVG longer

rather than using temporary png files and combining them together
later, merge files together in SVG and then just render once
This commit is contained in:
Adam Goldsmith 2017-08-03 19:03:17 -04:00
parent 0da1960ed6
commit edb8e40988
1 changed files with 16 additions and 14 deletions

View File

@ -7,15 +7,14 @@ import subprocess
import sys
from lxml import etree
from PIL import Image
if getattr(sys, 'frozen', False): # we are running in a bundle
bundle_dir = sys._MEIPASS
else: # we are running in a normal Python environment
bundle_dir = os.path.dirname(os.path.abspath(__file__))
CARD_WIDTH = 181 * 4
CARD_HEIGHT = 253 * 4
CARD_WIDTH = 181
CARD_HEIGHT = 253
def setText(tree, id, text):
element = tree.find('.//*[@id="' + id + '"]')
@ -58,14 +57,11 @@ def makeSVG(base, properties):
return tree
def addCardToBase(svg, baseImg, baseX, cardNum):
# TODO: possible to remove all this writing to temp files?
dest = "out/fig" + str(cardNum)
svg.write(dest + ".svg")
subprocess.call(["inkscape", "-z", "-f", dest + ".svg", "-w",
str(CARD_WIDTH), "-e", dest + ".png"])
cardImg = Image.open(dest + ".png")
baseImg.paste(cardImg, ((cardNum % baseX) * cardImg.width,
int(cardNum / baseX) * cardImg.height))
for e in svg.findall('{http://www.w3.org/2000/svg}g'):
e.set("transform", e.get("transform", "") + " translate(" + \
str((cardNum % baseX) * CARD_WIDTH) + " " + \
str(int(cardNum / baseX) * CARD_HEIGHT) + ")")
baseImg.getroot().append(svg.getroot())
def makeFace(baseImage, baseX, cardNum, base, card):
fig = makeSVG(base, card)
@ -73,8 +69,12 @@ def makeFace(baseImage, baseX, cardNum, base, card):
def makeFaces(deckJson, outfile):
baseX = math.ceil(math.sqrt(len(deckJson['deck']) + len(deckJson['character']) * 2))
baseImage = Image.new('RGB', (CARD_WIDTH * baseX,
CARD_HEIGHT * baseX))
baseImage = etree.ElementTree(
etree.Element('svg',
attrib={'width': str(baseX * CARD_WIDTH) + "pt",
'height': str(baseX * CARD_HEIGHT) + "pt",
'version': "1.2",
'xmlns': "http://www.w3.org/2000/svg"}))
cardType = deckJson["type"]
@ -121,7 +121,9 @@ def makeFaces(deckJson, outfile):
os.path.join("images", cardType, "card.svg"), card)
cardNum += 1
baseImage.save(outfile + ".png", "PNG")
baseImage.write(outfile + ".svg")
subprocess.call(["inkscape", "-z", "-f", outfile + ".svg", "-w",
str(baseX * CARD_WIDTH * 4), "-e", outfile + ".png"])
return baseX
def makeCardJson(template, nickname, description, cardID):