如何将名称为负数的图像转换为animation/video?

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

我根据一系列静态图像创建 animation ( gif) 或视频。图像以实数作为名称,例如-1.000.pgm

当数字为正数(没有负号)时有效

    #!/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

但是当数字为负数时,gif 中的顺序是错误的。 我试过将名称更改为正整数:

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

没有好结果。

我该怎么做?

============= 编辑 1===================

Maxxim 回答后的结果:

我认为字母排序也在for循环中,所以也应该改变那里

===============编辑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=



这似乎是一个排序问题。您当前使用 *.gif,它被扩展为 alphabetically 排序的文件列表。但是你需要一个 numerically 排序的文件列表来实现你的目标。此外,您的区域设置会影响排序(请参阅 man sort)。

试试这个:

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

或者这个:

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

而不是:

convert *.gif -resize 600x600 a600_100.gif