2

I need to convert from svg to png all the images in a folder. Lets say that images are called test1.svg, test2.svg, ... , testn.svg. Using the following script:

for i in *.svg
do
    convert "$i" PNG24:"$i".png
done

does the job correctly, and the images are called test1.svg.png, test2.svg.png, ... , testn.svg.png. My questions are:

1) Is it possible to make the output images be called test1.png, test2.png, ... , testn.png, essentially removing the 'svg' part from the name?

2) Is it possible to send them directly into some other directory?

Thanks!

TheRevanchist
  • 331
  • 1
  • 4
  • 12

1 Answers1

5

Yes. You can make another directory and send them there like this:

mkdir other
for i in *.jpg; do
   convert "$i" PNG24:other/"${i%jpg}png"
done

If you have lots of images to do, and you are on macOS or Linux, I would recommend GNU Parallel to get the job done faster:

mkdir other
parallel convert {} PNG24:other/{.}.png ::: *jpg
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 1
    Neat. The parallel part seems fantastic to me. Many thanks! – TheRevanchist Jul 17 '17 at 09:24
  • It is a brilliant tool if you have lots to do and want to make use of all those lovely Intel cores you paid for and which normally sit there idle. Try adding `--progress` or `--bar` to see progress. You can also use `--dry-run` to see what it would do with actually doing anything. 10 minutes spent here is well worthwhile https://www.gnu.org/software/parallel/parallel_tutorial.html Good luck with your project. – Mark Setchell Jul 17 '17 at 09:32
  • Mark Setchell: Thanks for the tip about ${string%substring} Strip shortest match of $substring from back of $string. In ImageMagick, I had been in the habit of doing: name=$(convert -ping $file -format "%t" info:) to remove the suffix. Now I can just do name=${file%.*} – fmw42 Jul 17 '17 at 17:40
  • @fmw42 Cool - there are loads of them actually, you can strip from start or end, substitute in the middle, take substrings etc. I get them by Googling *"tldp parameter substitution"* which brings you to here... http://tldp.org/LDP/abs/html/parameter-substitution.html – Mark Setchell Jul 17 '17 at 18:12
  • Nice commands. Oddly enough, `for i in *.svg; do; convert $i ${i%svg}gif; done;` takes **26 seconds** for 29 ~40kB SVGs, while `parallel convert {} {.}.gif ::: *svg` pegs 10 M1 Max cores at 100% for **389** seconds, for the same outcome! Maybe magick works some magic by itself? – Heath Raftery Dec 19 '22 at 05:56
  • @HeathRaftery As with all things, you need to check your actual setup, data and environment. **ImageMagick** has to rasterise SVGs, so your 40kB could correspond to a 10000x2000 PNG that takes so much RAM that your system has to swap when multiple images are loaded in parallel, for example. – Mark Setchell Dec 19 '22 at 08:38
  • lol, at 32MB per process on a 64GB machine, with a 15kB output file size, I think we're good. I'm not trying to prove anything, I just thought it was surprising enough to share. Feel free to check yourself: run command on 6 copies, <3s; on 8 copies, >28 seconds. https://temp.sh/zrDBB/sensor1.svg – Heath Raftery Dec 19 '22 at 10:27
  • @HeathRaftery I get 24s to run 29 conversions sequentially. Then 14s to run the same thing with `parallel -j2`. Then 9s to run same thing with `parallel -j4`. Then 15s to run with `parallel -j8` so there is something non-linear and hitting the buffers somewhere. – Mark Setchell Dec 19 '22 at 11:26
  • 1
    Aha, if I use `parallel -j8 magick -limit thread 1 ....` it stops OpenMP using more than a single thread and does all 29 images in 5s. – Mark Setchell Dec 19 '22 at 11:31