How do I force the output format?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

How do I force the output format?

Post by rmagick »

I'm using the MagickCore API.

Suppose I know I want the format of an image to be JPEG and I want to write the image to a file named "tmp/test.6543.0". How do I make WriteImage write the image in JPEG format?

Currently I store "JPEG" in the image_info->magick field and then call SetImageInfo, but the trailing ".0" on the filename confuses SetImageInfo into setting the image_info->magick field to "0". If I skip the call to SetImageInfo, leaving image_info->magick set to "JPEG" and image->magick set to the current image format, then WriteImage writes the image in the current format, not JPEG.

What am I doing wrong?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How do I force the output format?

Post by anthony »

write the image to a filename with a 'format prefix. For example "jpg:tmp/test.6543.0". The prefix is not written, but overides any trailing suffix in the filename.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: How do I force the output format?

Post by rmagick »

Well, I don't really have any control over the output filename. It would be nice to be able to specify the output format via some other channel, like the image_info->magick field.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How do I force the output format?

Post by anthony »

Just take the output filename, and copy it to a new string with the prefix, before using it. Otherwise insist that the supplier of the filename produces one with the right suffix!!!

I mean you really shouldn't have files with bad suffixes, even temporary files. It is bad programming and data handling.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: How do I force the output format?

Post by rmagick »

Thanks for your reply, Anthony. I don't think I'll have much luck persuading the Ruby folks to change the format of temporary file names, so I suppose I'll have to go the route of adding the format as a prefix to the filename. I want to think about what to do in the case that the program specifies the "JPEG" format and the output filename is "gif:foo.bar".
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How do I force the output format?

Post by anthony »

You should be able to use "jpg:gif:foo.bar" testing...

Code: Select all

   convert rose: jpg:gif:foo.bar
It created the file gif:foo.bar

It is only some special output formats (like histogram: ) that would look for another prefix after its own.. EG: -format '%c' histogram:info:- See http://imagemagick.org/Usage/files/#histogram

Code: Select all

file gif:foo.bar
Shows it is indeed a JPEG image file!!!

Code: Select all

identify gif:foo.bar
identify works too.

Looks like image input first trys the given string as a filename before looking for a format prefix hint! to define the type of file.
Of course you can fix that by using file redirection
In shell this is...

Code: Select all

identify - < gif:foo.bar
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply