Search code examples
rubyrmagick

RMagick unable to open image


Today i try to write a code for generate animated gif from folder but i get this error:

.../.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>'

This is the code:

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

Thanks a lot ;)


Solution

  • ImageList.new expects file names as arguments, one at a time. You are providing only one argument, string of concatenated file names. So instead of

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

    use

    animation = ImageList.new *files
    

    the *files instructs ruby to expand the array members into separate arguments