1

今天我尝试编写一个从文件夹生成动画 gif 的代码,但我收到了这个错误:

.../.gem/ruby/2.0.0/gems/rmagick-2.13.2/lib/RMagick.rb:1635:in `read': unable to open image `0.jpg,3.jpg,1.jpg,2.jpg': No such file or directory @ error/blob.c/OpenBlob/2641 (Magick::ImageMagickError)
from .../.gem/ruby/2.0.0/gems/rmagick-2.13.2/lib/RMagick.rb:1635:in `block in initialize'
.../.gem/ruby/2.0.0/gems/rmagick-2.13.2/lib/RMagick.rb:1634:in `each'
.../.gem/ruby/2.0.0/gems/rmagick-2.13.2/lib/RMagick.rb:1634:in `initialize'
from .../bin/scripts/animate.rb:20:in `new'
from .../bin/scripts/animate.rb:20:in `<main>'

这是代码:

#!/usr/bin/env ruby

require 'RMagick'
include Magick

files = Dir.glob"*.jpg"

if (!ARGV[0])
 puts "Usage:"
 puts "animate.rb 10"
 puts "-0 delay ex. 10"
else
  f = files.join(',').to_s
  animation = ImageList.new("#{f}")
  animation.delay = ARGV[0].to_i
  animation.write("animated.gif")
 exit
end

非常感谢 ;)

4

1 回答 1

3

ImageList.new 需要文件名作为参数,一次一个。您只提供一个参数,即连接文件名的字符串。所以而不是

f = files.join(',').to_s
animation = ImageList.new("#{f}")

利用

animation = ImageList.new *files

指示 ruby​​ 将*files数组成员扩展为单独的参数

于 2013-04-19T16:16:21.700 回答