abey79 / vpype

The Swiss-Army-knife command-line tool for plotter vector graphics.
https://vpype.readthedocs.io/
MIT License
699 stars 61 forks source link

Feature request: batch file input #5

Closed zoso95 closed 3 years ago

zoso95 commented 4 years ago

I think a killer feature would be to have something like pythons enumerate ontop of a directory

What I am imagining is something like

files = a directory, or user generated list for i, file in files: do stuff

I think this would make handling layers easier (right now I just use a shell variable for each file cause), and it would let you do custom grid stuff in a nifty way (today I went to grid some files and my solution ended up being)

read f1

translate 0mm 110mm

read f2

translate 0mm 110mm

...

show

but instead you could do

for i, file in directory: read file -l i

and for i, file in files: read file translate (i/num_files), (i%num_files)

duncangeere commented 4 years ago

I use the following shell script for batch processing using vpype - just run it in a directory full of svgs.

#!/bin/bash

echo "Starting SVG file processing"

# Loop over all the files
for file in *.svg; do
  echo "Processing $file"

  # Insert the vpype command you want to use for batch processing here
  vpype read "$file" write --page-format 18cmx12cm --center "export/$file"

done

echo "Processing complete"
abey79 commented 3 years ago

I just added a section on this on the cookbook, with a twist to use parallel for multiprocessing.

I will close this issue for now because I see no avenue for doing this in a way that is a significant improvement over bash scripting, etc.

abey79 commented 3 years ago

I'm reopening this to discuss potential improvement as this is being requested again (#107)

As mentioned above, it's relatively easy to process lots of files with GNU parallel, with the added benefit of "free" multiprocessing. There may be some smaller steps that could be made to make the process easier and, ideally, without the need for a bash script.

Some ideas: 1) "remember" file naming from read and use it to accept templates in write 2) make a secondary vpype-batch CLI that would accept wildcards in parameters and run multiple instance of vpype 3) build inside vpype the batch capability

abey79 commented 3 years ago

I dug a bit more into GNU parallel, and it's relatively easy to make a one-liner:

parallel --plus vpype read {} linemerge linesort write {/.svg/_processed.svg} ::: *.svg

Notes that --plus is needed to enable fancy argument processing, in this case {/.svg/_processed.svg} to remplace eg. my_file.svg to my_file_processed.svg. Dynamic replacements are documented here.