Issue
scour for Python does a great job to reduce the size of the SVG maps that I have generated using matplotlib.
However, the documentation only describes how to run it for a single file from the CLI, standard:
scour -i input.svg -o output.svg
What is the best way to run it (in batch mode) from a Python script / from my Jupyter notebook?
I succeeded to 'create' the working code below and optimized 500 SVGs to my satisfaction. But I just copied that code in bits from the testscour.py and I lack the understanding about it...
from scour.scour import makeWellFormed, parse_args, scourString, scourXmlFile, start, run
maps= os.listdir('C:\\Hugo\\Sites\\maps\\')
for m in maps[:]:
if afbeelding[-4:]=='.svg':
inputfile = open('C:\\Hugo\\Sites\\maps\\' + m, 'rb')
outputfile = open('C:\\Hugo\\Sites\\allecijfers\\static\\images\\maps\\' + m, 'wb')
start(options, inputfile, outputfile)
Related questions (trying to learn): What is the best way to decompose a module like scour in order to find the right callable functions? (I would not have found the function start in scour.py ) Or could I have called the CLI command from the Python script above in order to execute it in batch mode?
Solution
I like your question because it pointed me to testscour.py, thanks a lot.
That said, the code imports scour first, then makes makes a list from your map folder; then goes through the list one by one, if it is an svg, it uses the "start" method to call scour.
so the working piece is
start(options, inputfile, outputfile)
Note that it needs an opened file handler, usually you make that with "with".
Have fun
Answered By - Anderas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.