【问题标题】:Defining the file order for ImageMagick convert定义 ImageMagick 转换的文件顺序
【发布时间】:2012-09-02 14:09:51
【问题描述】:

我有一堆名为foo<bar>.pngPNG 文件,我希望转换为TIF 动画。 <bar> 是一个从 0 到 25 变化的数字,以 5 为单位。 ImageMagick 将foo5.png 放在动画的最后,而它应该是第二个。除了将文件重命名为foo05.png 之外,还有什么方法可以将其放置在正确的位置?

【问题讨论】:

  • 用数字前导零重命名图像,例如 foo005.png。通配符 * 按字母顺序而非数字顺序读取图像。因此,前导零将导致您的文件名在被通配符访问时按字母顺序列出。

标签: imagemagick imagemagick-convert


【解决方案1】:

如果您的输入图像多于无法输入的方便程度(例如 foo0..foo100.png),您可以这样做(在 Linux、Unix 和 Mac OS X 上):

convert                                                  \
  -delay 10                                              \
   $(for i in $(seq 0 5 100); do echo foo${i}.png; done) \
  -loop 0                                                \
   animated.gif

【讨论】:

  • 请注意,您也可以使用它来反转 GIF 的顺序:$(seq 100 -5 0)
  • 注意如果你只是想颠倒正常的顺序你可以使用这个方法但是用$(ls -r *.png)替换子命令。
【解决方案2】:

简单易行,列出您的图像并对其进行排序:

convert -delay 10 -loop 0 $(ls -1 *.png | sort -V) animated.gif

【讨论】:

    【解决方案3】:

    您只需按照它们应该出现在动画中的顺序给出您的 PNG 文件的顺序。使用:

    foo0.png foo5.png foo10.png foo15.png foo20.png foo25.png
    

    而不是

    foo*.png
    

    毕竟,只有 6 个不同的文件名应该很容易输入:

    convert                                                      \
      -delay 10                                                  \
       foo0.png foo5.png foo10.png foo15.png foo20.png foo25.png \
      -loop 0                                                    \
       animated.gif
    

    【讨论】:

    • @Yotam:你看到我的其他答案了吗? 'upvote' 还不够好吗?
    • 在文件名的数字部分使用前导零编写此类文件很有帮助;然后你可以说“*.png”并按正确的顺序获取它们。即 foo00.png foo05.png foo15.png foo20.png foo25.png.
    【解决方案4】:

    您可以将“查找”与“排序”一起使用:

    convert -delay 10 $(find . -name "*.png" -print0 | sort -zV | xargs -r0 echo) -loop 0 animated.gif
    

    【讨论】:

    • 为什么使用find 而不是ls?为什么xargs?请参阅我的答案以获得更简单的解决方案。
    【解决方案5】:

    或者,如果您对 python 有所了解,那么您可以轻松地从 python shell 中获得它的帮助。

    通过在终端中输入python 来打开python shell。并应用以下魔法-

    # Suppose your files are like 1.jpeg, 2.jpeg etc. upto 100.jpeg
    files = []
    for i in range(1, 101):
        files.append('{}.jpeg'.format(i))
    command = 'convert -delay 10 {} -loop 0 animated.gif'.format(' '.join(files))
    from subprocess import call
    call(command, shell=True)
    

    你的工作应该完成了!

    【讨论】:

      【解决方案6】:

      比 ls 和 sort 更简单的是使用 ls 的内置 -v 选项:

      convert -delay 10 -loop 0 `ls -v *.png` animated.gif

      `...` 被执行而不是解释为字符串。

      【讨论】:

        猜你喜欢
        • 2017-02-25
        • 1970-01-01
        • 2017-07-16
        • 2015-05-03
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多