1

I have a bunch of pictures that I want to print, but in order for them to print out correctly every other picture needs to be flipped horizontally. Now, the pictures are all png files and are labeled "Foo 001.png", "Foo 002.png". I am thinking that all I need to do is look at the part of the string that has the number, convert it to an integer, do n%2==0 to check if it is even, and flip it if it is even.

I don't know if there is a convenient way to convert from a string to an integer using bash or what I would use to flip the picture.

4
  • Nah, just have a variable that you keep on toggling as you iterate over the files, no dependency on file naming convention then. X=0 ; ... ; do X=$((1-$X)) ; if [ $X -eq 1 ] ; then ... fi; ... done
    – vladr
    Nov 14, 2012 at 3:51
  • 1
    Not sure I understand. Do you want to flip 1/2 images ? By flip, do you mean rotate ? Nov 14, 2012 at 3:52
  • @vladr That would probably be easier, but how do I know what order the files will be iterated over in? Would it be alphabetical? Nov 14, 2012 at 3:55
  • @russjohnson09 ls, glob etc. will do it alphabetically. It's up to you to come up with a properly sorted list.
    – vladr
    Nov 14, 2012 at 3:57

3 Answers 3

2

Use a toggling variable to flip every other image in a list.

X=0

cat filenames | while read -r filename ; do
  if [ $X -eq 1 ] ; then
    # flip $filename
  fi
  # print $filename
  X=$((1-$X))
done

You may replace cat filenames above with ls -1 *.jpg to print *.jpg files in alphabetical order, or ls -1v to version-sort them, a way that does not require the sequence numbers in the filenames to be zero-padded.

If there are thousands of files, a combination of ls (no wildcards) and grep or one of find and sort can be used instead of the above in order to avoid blowing the ARG_MAX limit.

1

Since bash is much better with strings than numbers, my first thought would be

flip Foo\ *{0,2,4,6,8}.png

where flip is whatever command you're using to flip the pictures.

This assumes that the command can accept multiple filenames. If not, you can use a loop:

for f in Foo\ *{0,2,4,6,8}.png; do flip "$f"; done
1

If I understand well, you want to flip 1/2 png image ? If yes, then :

convert -flip "Foo *{0,2,4,6,8,10}.png"

or if you have too much files :

# making an array of files
files=( *.png )

# C style for loop (iterating 2 by 2)
for ((i=0; i< ${#files[@]}; i+=2)) {
     # fliping the image
     convert -flip "${files[i]}"
}

convert command is part of well known imagemagick library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.