RMagick problem: Image::read works well but Image::from_blob fails

Discussion in 'Ruby' started by Mike Meng, Sep 16, 2006.

  1. Mike Meng

    Mike Meng Guest

    Hi all,
    I want to determine the geometry of an image. The image file may be
    in local harddisk or on web. So I use open-uri and
    Magick::Image::from_blob to do that work, here is the code:

    open('<path or URI of image file>') { |f|
    image = Magick::Image::from_blob(f.read).first
    puts "Width: #{image.columns}; Height: #{image.rows}"
    }

    For most of the cases, this works well. However, we I tried to read an
    JPEG generated by my Ricoh Caplio digital camera, it failed and emitted
    these message:

    ---------- code ----------
    E:/workspace/ruby/idapted/testrmg.rb:6:in `from_blob': JPEG datastream
    contains
    no image `' (Magick::ImageMagickError)
    ....
    -----------------------------

    Surprisingly enough, if I use Magick::Image::read method to read the
    same image file directly from harddisk, everything is fine, here is the
    code:
    ---------- code ----------
    g = Magick::Image::read('<local file only>').first
    puts "Width: #{g.columns}; Height: #{g.rows}"
    -----------------------------

    Why do Image::read and Image::from_blob make differeces? How to
    determine the geometry of a remote image?

    Any comment is appreciated. Thanks!
    Mike Meng, Sep 16, 2006
    #1
    1. Advertising

  2. Re: RMagick problem: Image::read works well but Image::from_blobfails

    Mike Meng wrote:
    > Hi all,
    > I want to determine the geometry of an image. The image file may be
    > in local harddisk or on web. So I use open-uri and
    > Magick::Image::from_blob to do that work, here is the code:
    >
    > open('<path or URI of image file>') { |f|
    > image = Magick::Image::from_blob(f.read).first
    > puts "Width: #{image.columns}; Height: #{image.rows}"
    > }
    >
    > For most of the cases, this works well. However, we I tried to read an
    > JPEG generated by my Ricoh Caplio digital camera, it failed and emitted
    > these message:
    >
    > ---------- code ----------
    > E:/workspace/ruby/idapted/testrmg.rb:6:in `from_blob': JPEG datastream
    > contains
    > no image `' (Magick::ImageMagickError)
    > ....
    > -----------------------------
    >
    > Surprisingly enough, if I use Magick::Image::read method to read the
    > same image file directly from harddisk, everything is fine, here is the
    > code:
    > ---------- code ----------
    > g = Magick::Image::read('<local file only>').first
    > puts "Width: #{g.columns}; Height: #{g.rows}"
    > -----------------------------
    >
    > Why do Image::read and Image::from_blob make differeces? How to
    > determine the geometry of a remote image?
    >
    > Any comment is appreciated. Thanks!
    >
    >
    >

    Under the covers, #read and #from_blob share most of their code. If
    Image.read can read the file then Image.from_blob can handle it, too.
    The argument to Image.from_blob must be a string containing the entire
    image. I'm guessing that the argument you're giving it is only part of
    the image. Assign f.read to a variable and display its length. Is it the
    same as the size of the image file on disk?
    Timothy Hunter, Sep 16, 2006
    #2
    1. Advertising

  3. Mike Meng

    Mike Meng Guest

    Re: RMagick problem

    Thanks to Paul and Timothy.

    I've found the problem. I made a stupid mistake when open the URI of an
    image file without "b" flag. With "b" flag, everything is ok.

    Thanks again.

    Mike

    Timothy Hunter 写é“:

    > Mike Meng wrote:
    > > Hi all,
    > > I want to determine the geometry of an image. The image file may be
    > > in local harddisk or on web. So I use open-uri and
    > > Magick::Image::from_blob to do that work, here is the code:
    > >
    > > open('<path or URI of image file>') { |f|
    > > image = Magick::Image::from_blob(f.read).first
    > > puts "Width: #{image.columns}; Height: #{image.rows}"
    > > }
    > >
    > > For most of the cases, this works well. However, we I tried to read an
    > > JPEG generated by my Ricoh Caplio digital camera, it failed and emitted
    > > these message:
    > >
    > > ---------- code ----------
    > > E:/workspace/ruby/idapted/testrmg.rb:6:in `from_blob': JPEG datastream
    > > contains
    > > no image `' (Magick::ImageMagickError)
    > > ....
    > > -----------------------------
    > >
    > > Surprisingly enough, if I use Magick::Image::read method to read the
    > > same image file directly from harddisk, everything is fine, here is the
    > > code:
    > > ---------- code ----------
    > > g = Magick::Image::read('<local file only>').first
    > > puts "Width: #{g.columns}; Height: #{g.rows}"
    > > -----------------------------
    > >
    > > Why do Image::read and Image::from_blob make differeces? How to
    > > determine the geometry of a remote image?
    > >
    > > Any comment is appreciated. Thanks!
    > >
    > >
    > >

    > Under the covers, #read and #from_blob share most of their code. If
    > Image.read can read the file then Image.from_blob can handle it, too.
    > The argument to Image.from_blob must be a string containing the entire
    > image. I'm guessing that the argument you're giving it is only part of
    > the image. Assign f.read to a variable and display its length. Is it the
    > same as the size of the image file on disk?
    Mike Meng, Sep 17, 2006
    #3
  4. Mike Meng

    Yi Zheng Guest

    Re: RMagick problem

    Mike Meng wrote:
    > Thanks to Paul and Timothy.
    >
    > I've found the problem. I made a stupid mistake when open the URI of an
    > image file without "b" flag. With "b" flag, everything is ok.
    >
    > Thanks again.
    >
    > Mike
    >
    > Timothy Hunter �??�?�?



    Hi Mike

    I have the similar issue, please check my post:
    http://www.ruby-forum.com/topic/164823#new

    thanks

    Yi
    --
    Posted via http://www.ruby-forum.com/.
    Yi Zheng, Sep 7, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Marsh
    Replies:
    2
    Views:
    311
    Peter Marsh
    Nov 4, 2003
  2. Aaron Johnson
    Replies:
    0
    Views:
    174
    Aaron Johnson
    Nov 2, 2005
  3. Ashwin Kumar
    Replies:
    1
    Views:
    175
    Tim Hunter
    Jun 25, 2007
  4. Gaspard Bucher

    RMagick write fails / to_blob works

    Gaspard Bucher, Aug 25, 2008, in forum: Ruby
    Replies:
    7
    Views:
    185
    Gaspard Bucher
    Aug 26, 2008
  5. Pito Salas
    Replies:
    1
    Views:
    137
    Tim Hunter
    Apr 24, 2009
Loading...

Share This Page