You are not logged in.

#1 2010-01-01 14:38:50

Valiant
Member
Registered: 2009-11-10
Posts: 15

tweak for bash script to change numbering in sequential files

Hello,

I use Lilypond on a weekly basis to produce music that eventually ends up in powerpoint presentations.  I have a Lilypond template that gets close to the proportions needed.  Since Lilypond doesn't properly carry over the page height when converting to png files, I've always had to manually crop and convert to alpha for each slide image used. which is a tedious process.  Today, I found a bash script online and modified it a bit to convert a multipage pdf music file into individual png files using the imagemagick convert command. 

#!/bin/bash
for i in `ls *.pdf`; do convert "$i" -density 90x90 -crop 1024x768+0+0\! +repage -transparent white "$i".png; done

The only issue that remains is that the script creates files named <filename>.pdf-0.png, <filename>.pdf-1.png, <filename>.pdf-2.png, etc.
When inserting the png files into OpenOffice Impress, I'm always now trying to insert mismatched files - a "0" file into the first slide, a "1" file into the second slide, etc. Imagemagick doesn't seem to provide a way to modify this behavior, so I'm looking for a way to modify these files once they have been created.  Ideally, the fix would remove the embedded and confusing ".pdf" and replace the existing digit(s) with a number +1 increment higher.  So score.pdf-0.png would become score-1.png and so on.  If there are any scripting gurus out there with the solution on the tip of their fingers, I'd appreciate a pointer or two.  It can work the way it is now.  But I've come so far today and am just hoping to get it perfect.  Thanks for any help.

Offline

#2 2010-01-01 15:31:10

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: tweak for bash script to change numbering in sequential files

Try this on some dummies:

perl -e 'foreach (<stdin>) {print $_ . " $1-".($2+1).".png\n" if (/(.*)\.pdf-(\d+).png/)}'

e.g. something like

$ ls *.png
qglkjx.pdf-2.png  saonet.pdf-1.png x.png

$ ls *.pdf-*.png | perl -e 'foreach (<stdin>) {print $_ . " $1-".($2+1).".png\n" if (/(.*)\.pdf-(\d+).png/)}'|xargs -n2 mv

$ ls *.png
qglkjx-3.png  saonet-2.png x.png

I'm very inexperienced with perl, but I chose it because of the needed addition is difficult to embed in sed (which lack math functions) and I dont know/like awk. It should do the trick though.

Last edited by tlvb (2010-01-01 15:31:40)


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#3 2010-01-01 20:50:48

Valiant
Member
Registered: 2009-11-10
Posts: 15

Re: tweak for bash script to change numbering in sequential files

Worked like a charm.  Thank you very much and Happy New Year!

Offline

#4 2010-01-02 23:56:07

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: tweak for bash script to change numbering in sequential files

Hello,

Here is how I did it in bash:

If you change your command to:

#!/bin/bash
for i in *.pdf ; do convert "$i" -density 90x90 -crop 1024x768+0+0\! +repage -transparent white "$i" ; done

then you can do:

j=0 ; for i in *.pdf* ; do j="${i#*-}" ; ((j++)) ; mv "$i" "${i%.pdf*}"-"$j".png ; done

Last edited by steve___ (2010-01-03 04:07:13)

Offline

Board footer

Powered by FluxBB