How to Read from Stdin with ImageMagick Convert?

This article provides a quick overview and practical guide on how to use the ImageMagick convert command (or the magick command in newer versions) to process images directly from standard input (stdin). Passing data via stdin allows you to seamlessly pipeline image processing tasks with other command-line utilities without saving intermediate files to your disk. You will learn the specific syntax required, how to handle different image formats, and see real-world examples of piping data into ImageMagick.

The Standard Input Syntax (-)

To tell ImageMagick to read from standard input instead of a physical file on your disk, you use a single hyphen (-) in place of the input filename.

cat input.jpg | convert - output.png

In this basic example, cat sends the image data through a pipe, and convert - catches that data from stdin, processes it, and saves it as output.png.

Specifying the Image Format

When reading from a file, ImageMagick automatically detects the file type using its extension (like .jpg or .png). When you stream data through stdin, ImageMagick will still try to automatically detect the format based on the file’s unique magic bytes (the internal header data).

However, if you are working with raw data formats or if the auto-detection fails, you must explicitly tell ImageMagick what format to expect by prefixing the hyphen with the format type and a colon:

curl -s https://example.com/image | convert png:- -resize 50% output.jpg

In the command above, png:- explicitly instructs ImageMagick to treat the incoming standard input stream as a PNG image.

Practical Examples

1. Piping from a Web Request

You can download an image using curl or wget and process it instantly without writing the original file to your hard drive.

curl -s "https://example.com/logo.png" | convert - -thumbnail 100x100 thumb.png

2. Chaining Multiple ImageMagick Commands

While you can usually do multiple operations in a single ImageMagick command, you can also pipe the standard output (stdout) of one command into the standard input (stdin) of another by using a hyphen for both output and input.

convert input.jpg -crop 500x500+0+0 miff:- | convert - -colorspace Gray output.jpg

Note: When piping between ImageMagick commands, using the miff:- (Magick Image File Format) is highly recommended because it preserves all image metadata and quality perfectly across the pipe.

3. Base64 Decoding Directly to ImageMagick

If you have an image stored as a base64 string, you can decode it and pass it straight to ImageMagick via stdin.

echo "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" | base64 --decode | convert - output.png