【问题标题】:How to convert images with negative number as a name to animation/video?如何将带有负数作为名称的图像转换为动画/视频?
【发布时间】:2021-12-09 22:55:17
【问题描述】:

我从一系列静态图像创建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=



【问题讨论】:

  • for 循环中的排序无关紧要,因为它只会将所有 .pgm 文件转换为 .gif 文件(因此只要处理所有文件,顺序就无关紧要) .首先如何获得带有负数的文件?我自己跑了e.she.c,我没有得到任何带有负数的文件。
  • @Maxxim 在 e.c 的主函数中更改 double t0 = 0.0 ;到 t0=-1 或 -2
  • 使用double t0 = -1.0,由于错误,我不得不将行b=$(basename $file .pgm) 更改为b=$(basename -- $file .pgm),并按照我的回答中所述进行动画 gif 的转换。有了这个,我得到了一个完美播放的a600_100.gif。如果这对您不起作用,您能否上传printf '%s\n' *.gif | sort -n 的输出?

标签: bash sorting locale imagemagick-convert basename


【解决方案1】:

这似乎是一个排序问题。您当前使用*.gif,它扩展为alphabetically 排序的文件列表。但是您需要在此处按数字排序的文件列表来实现您的目标。此外,您的区域设置会影响排序(请参阅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

【讨论】:

  • 什么对我有用:readarray -t files
  • @Adam:是的,我在一分钟前也发现了同样的事情。所以问题是排序+语言环境。我稍后会更新我的答案。
  • @Adam:我扩展了我的答案以包括有关区域设置的部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 2011-08-08
  • 2017-02-26
  • 2018-07-16
相关资源
最近更新 更多