GFL
GFL

Reputation: 1424

Convert image from one format to another sent to STDOUT

I'd like to convert an image from .jpg to .png. This works just fine:

convert input.jpg output.png

But, I'm trying to have my output go to STDOUT instead of a file, which the manual says to use "-".

I tried using:

convert input.jpg -.png

But it just creates a file called -.png.

Is it possible to convert an image from one format to another and have it go to STDOUT?

Upvotes: 5

Views: 2368

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207365

Yes, just use a command like this to convert a JPEG to a PNG on stdout:

magick input.jpg PNG:-

These specifiers work on input as well as output. So, if you have a TIFF on stdin and want a 32-bit RGBA PNG on stdout:

magick TIFF:- PNG32:-

You often need these specifiers to ensure a specific filetype when it is not explicitly given, or you want to use a different extension. So, say you have some CCD device that produces RGB data in a raw binary file called image.bin and you want ImageMagick to read it. You can tell ImageMagick the format without having to change the filename (to image.rgb) like this:

magick -size WxH RGB:image.bin result.png

The possible formats are documented here.


The king of all of these formats is MIFF which is guaranteed to be able to hold any and all things you might throw at it, including floating-point values, transparency, masks, concatenated streams... so if you need a format to pass between ImageMagick commands, MIFF is a good option. An example, just to demonstrate because it is not optimal, might to be to concatenate two images from 2 separate ImageMagick commands into a third command that makes an animated GIF:

{ magick -size 100x60  xc:red miff:- ; magick -size 100x60 xc:blue miff:- ; } | magick -delay 80 miff:- result.gif

enter image description here

Upvotes: 6

Related Questions