bashsortinglocaleimagemagick-convertbasename

How to convert images with negative number as a name to animation/video?


I create animation ( gif) or video from a sequence of static images. Images have real number as a name, for example -1.000.pgm

When numbers are positive ( without minus sign) then it works

    #!/bin/bash 
 
# script file for BASH 
# which bash
# save this file as e.sh
# chmod +x e.sh
# ./e.sh
# checked in https://www.shellcheck.net/




printf "make pgm files \n"
gcc e.c -lm -Wall -march=native -fopenmp

if [ $? -ne 0 ]
then
    echo ERROR: compilation failed !!!!!!
    exit 1
fi


export  OMP_DISPLAY_ENV="TRUE"
printf "display OMP info \n"

printf "run the compiled program\n"
time ./a.out > e.txt

export  OMP_DISPLAY_ENV="FALSE"

printf "change Image Magic settings\n"
export MAGICK_WIDTH_LIMIT=100MP
export MAGICK_HEIGHT_LIMIT=100MP

printf "convert all pgm files to png using Image Magic v 6 convert \n"

for file in *.pgm ; do
  # b is name of file without extension
  b=$(basename ./$file .pgm)
  # convert from pgm to gif and add text ( level ) using ImageMagic
  # https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
  convert $file -pointsize 50 -annotate +10+100 ${b:0:4} ${b}.gif
  echo $file
 
done
 
# convert gif files to animated gif
convert *.gif -resize 600x600 a600_100.gif


printf "delete all pgm files \n"
rm ./*.pgm

 
echo OK

but when number is negative then the order in gif is wrong. I have tried chang e name to positive integers :

i=0
for file in *.pgm ; do
  # b is name of file without extension
  b=$(basename ./$file .pgm)
  # convert from pgm to gif and add text ( level ) using ImageMagic
  # https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
  convert $file -pointsize 50 -annotate +10+100 ${b:0:4} ${i}.gif
  echo $file
  i=$((i + 1))
done

without good result.

How can I do it ?

============= edit 1===================

The result after Maxxim's answers :

enter image description here

I think that alphabetic sorting is also inside for loop, so it should be changed there also

=============== edit 2 ========================

printf '%s\n' *.gif | sort -n
-1.000000.gif
0.000000.gif
-0.200000.gif
0.200000.gif
-0.400000.gif
0.400000.gif
-0.600000.gif
0.600000.gif
-0.800000.gif
0.800000.gif
a200.gif
1.000000.gif

locale
LANG=pl_PL.UTF-8
LANGUAGE=
LC_CTYPE="pl_PL.UTF-8"
LC_NUMERIC="pl_PL.UTF-8"
LC_TIME="pl_PL.UTF-8"
LC_COLLATE="pl_PL.UTF-8"
LC_MONETARY="pl_PL.UTF-8"
LC_MESSAGES="pl_PL.UTF-8"
LC_PAPER="pl_PL.UTF-8"
LC_NAME="pl_PL.UTF-8"
LC_ADDRESS="pl_PL.UTF-8"
LC_TELEPHONE="pl_PL.UTF-8"
LC_MEASUREMENT="pl_PL.UTF-8"
LC_IDENTIFICATION="pl_PL.UTF-8"
LC_ALL=




Solution

  • This appears to be a matter of sorting. You currently use *.gif, which is expanded into an alphabetically sorted list of files. But you need a numerically sorted list of files here instead to achieve your goal. Additionally, your locale settings affect sorting (see man sort).

    Try this:

    readarray -t files < <(printf '%s\n' *.gif | LC_ALL=C sort -n)
    convert "${files[@]}" -resize 600x600 a600_100.gif
    

    or this:

    readarray -t files < <(find . -maxdepth 1 -type f -name '*.gif' -printf "%f\n" | LC_ALL=C sort -n)
    convert "${files[@]}" -resize 600x600 a600_100.gif
    

    instead of:

    convert *.gif -resize 600x600 a600_100.gif