0

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=



Adam
  • 1,254
  • 12
  • 25
  • The sorting within the `for` loop doesn't matter as it only converts all `.pgm` files to `.gif` files (thus the order doesn't matter as long as all files are processed). How do you get files with negative numbers in the first place? I ran `e.sh` and `e.c` myself and I don't get any files with negative numbers. – Fonic Oct 24 '21 at 10:05
  • @Maxxim In main function from e.c change double t0 = 0.0 ; to t0=-1 or -2 – Adam Oct 24 '21 at 10:11
  • With `double t0 = -1.0`, I had to change line `b=$(basename $file .pgm)` to `b=$(basename -- $file .pgm)` due to errors and do the conversion to animated gif like described in my answer. With that, I get an `a600_100.gif` that plays perfectly. If that doesn't work for you, could you please upload the output of `printf '%s\n' *.gif | sort -n` ? – Fonic Oct 24 '21 at 11:24
  • https://stackoverflow.com/questions/69695574/is-it-possibly-to-sort-files-numerically-when-nemas-arre-real-numbers – Adam Oct 24 '21 at 12:07

1 Answers1

2

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
Fonic
  • 2,625
  • 23
  • 20
  • what works for me : readarray -t files < <(printf '%s\n' *.gif | LC_ALL=C sort -n) convert "${files[@]}" -resize 600x600 a600.gif – Adam Oct 24 '21 at 11:55
  • @Adam: yes, I also discovered the same thing a minute ago. So the issue is sorting + locale. I'll update my answer later. – Fonic Oct 24 '21 at 11:57
  • @Adam: I extended my answer to include the part regarding locale settings. – Fonic Oct 24 '21 at 13:34