RMagick write fails / to_blob works

G

Gaspard Bucher

I am trying to use @img.write(path) but it's not working (zero bytes
written). Using "to_blob" works.


f = Tempfile.new('tmp_img')
FAILING CODE:
@img.write(f.path)

WORKING CODE:
File.open(f.path, 'wb') do |f|
f.syswrite(@img.to_blob)
end


Any idea ?

(RMagick 2.5.2)

Thanks for any reply.

Gaspard
 
T

Tim Hunter

Gaspard said:
I am trying to use @img.write(path) but it's not working (zero bytes
written). Using "to_blob" works.


f = Tempfile.new('tmp_img')
FAILING CODE:
@img.write(f.path)

WORKING CODE:
File.open(f.path, 'wb') do |f|
f.syswrite(@img.to_blob)
end

Image#write doesn't know what to do with a Tempfile object. It can only
accept a string or a File object that's been opened for output.

Image#write calls directly into ImageMagick, and ImageMagick doesn't
know anything about Ruby objects like Tempfiles.
 
G

Gaspard Bucher

Image#write's argument is in fact a String (f.path), not the Tempfile
object itself...
 
M

Michael Morin

Gaspard said:
Image#write's argument is in fact a String (f.path), not the Tempfile
object itself...
Image#write doesn't know what to do with a Tempfile object. It can only
accept a string or a File object that's been opened for output.

Image#write calls directly into ImageMagick, and ImageMagick doesn't
know anything about Ruby objects like Tempfiles.

It's possibly due to the file already being opened for writing. Try
f.close first, then @img.write(f.path).

--
Michael Morin
Guide to Ruby
http://ruby.about.com/
Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company
 
T

Tim Hunter

Gaspard said:
Image#write's argument is in fact a String (f.path), not the Tempfile
object itself...

I see. I overlooked that earlier.

Normally Image#write either succeeds or raises an exception. I have
never seen it silently fail.

If you like, open a support request track on RubyForge with a simple
reproduction (just Ruby and RMagick, no Rails, etc.) and let me know
what O/S you're on and what version of ImageMagick you have installed.
I'll be glad to investigate.
 
S

Serabe

Hi, Gaspard and Tim:

I've tried changing:

@img.write(f.path)

to

@img.write(f.path+'.jpg')

and it works. Maybe RMagick just needs to check if there is an
extension ImageMagick can use.

Cheers,

Serabe
 
T

Tim Hunter

Serabe said:
Hi, Gaspard and Tim:

I've tried changing:

@img.write(f.path)

to

@img.write(f.path+'.jpg')

and it works. Maybe RMagick just needs to check if there is an
extension ImageMagick can use.

Cheers,

Serabe

Duh. I had to figure this out just a few weeks ago. I should've
remembered. I must be getting old.

The Tempfile path ends in .0, which is not an extension ImageMagick can
use. As Serabe so intelligently points out, adding a valid extension
works. You can also add a prefix: "jpeg:" + f.path.

Thanks for being smarter than me, Serabe!
 
G

Gaspard Bucher

You guys rock !

Thanks very much. Adding 'jpeg:' prefix solved the problem. This =20
solution is better because it keeps the temporary filename so the file =20=

is still cleaned up.

Solution:

tmp_path =3D Tempfile.new('tmp_img').path
@img.write('jpeg:' + tmp_path) # writes to tmp_path

Thanks !

Gaspard

Le 26 ao=FBt 08 =E0 00:47, Tim Hunter a =E9crit :
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top