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