Page 1 of 2

convert image to two different sizes and pipe to stdout?

Posted: 2017-06-21T11:10:13-07:00
by maqe
Is it possible to resize an image to two different sizes, let's say one thumbnail and one large image then pipe them both to stdout? If so, how can I do that?

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-21T11:23:23-07:00
by snibgo
What version IM? I'll assume v6.

What platform? I'll assume Windows BAT.

What output format? Most formats can't simply be appended together. I'll assume MIFF.

Code: Select all

convert ^
  in.ext ^
  ( -clone 0 -resize 2000x2000 -write MIFF:- +delete ) ^
  ( -clone 0 -resize 10x10 -write MIFF:- ) ^
  NULL:

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-21T11:28:26-07:00
by maqe
I'm using IM running in a Docker container:

Version: ImageMagick 7.0.5-10 Q16 x86_64 2017-06-05 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher HDRI Modules
Delegates (built-in): fontconfig freetype gslib jng jpeg lcms ltdl png ps tiff webp xml zlib

If I use the suggested syntax, is the output two different streams or how does it work? I need to grab the stdoutput and save the files with different filenames

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-21T11:53:01-07:00
by snibgo
Sorry, I don't know what a Docker container is.

IM can save directly to files, and that's how it is normally used, eg:

Code: Select all

convert ^
  in.ext ^
  ( -clone 0 -resize 2000x2000 -write abc.png +delete ) ^
  ( -clone 0 -resize 10x10 -write def.jpg ) ^
  NULL:

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-21T13:17:55-07:00
by fmw42
Is your platform Linux or Windows? If Linux, the code snibgo provided will need some slight changes. I do not know Docker either. What output format do you want? If the format supports multiple pages such as TIFF, then you can write the two results to a single TIFF image format with two pages and pipe the output. Your receiving code would then have to separate the two pages. For example in Unix format

Code: Select all

convert in.suffix \( -clone 0 -resize 2000x2000 \) \( -clone 0 -resize 10x10 \) -delete 0 TIFF:- | your receiving code
For example using the ImageMagick internal image logo:

Code: Select all

convert logo: \( -clone 0 -resize 400x400 \) \( -clone 0 -resize 80x80 \) -delete 0 TIFF:- | convert - result.jpg
This produces two images result-0.jpg and result-1.jpg.

Replace logo: with yourimage.suffix

But as snibgo said, you can write your two images directly without the pipe as he has suggested above.

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-21T13:42:03-07:00
by maqe
The platform I'm using is Linux but my code is written in c# (dotnet core). The output format I want is JPG for both images.

How can I identify from the stdout where the first image ends end the second image starts?

This is the output from IM saved in a text file: https://d26dzxoao6i3hh.cloudfront.net/i ... 185390.txt

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-21T15:02:55-07:00
by fmw42
Sorry, that is something I cannot answer.

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-21T23:26:07-07:00
by maqe
It seems like the stream does not contain two images, it seems like the part to the right of | runs twice or something? How can I make sure that both images outputs to stdout and how can I catch them?

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-22T00:23:10-07:00
by snibgo
maqe wrote:It seems like the stream does not contain two images, it seems like the part to the right of | runs twice or something? How can I make sure that both images outputs to stdout and how can I catch them?
Please show the command you have used.

As I said, most formats can't simply be appended together. Appending two JPG files together makes a file that is not a valid JPG file. If the receiver is a program you have written yourself, it would need to parse the file to find the place to split it.

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-22T05:17:13-07:00
by maqe
I use this command:

Code: Select all

docker run imagemagick convert http://camendesign.com/code/video_for_everybody/poster.jpg \( -clone 0 -resize 400x400 -write JPG:- +delete \) \( -clone 0 -resize 80x80 -write JPG:- \) -
I don't know if I need the last pipe but I guess so. If I change it to

Code: Select all

docker run imagemagick convert http://camendesign.com/code/video_for_everybody/poster.jpg \( -clone 0 -resize 400x400 -write JPG:- +delete \) \( -clone 0 -resize 80x80 -write JPG:- \) - > poster.jpg
it only outputs a single image. This command is invoked from c# and I only get the stream output from the command. I don't think I can mimic the unix version of your example with a

Code: Select all

|
that runs twice because my c# code invokes the command a single time and the output from stdout is sent back to my c# code.

What I try to achieve is, I have my images in a Amazon S3 bucket and I need to create two versions of the image. One thumbnail and one larger image for preview purpose. I think it's downloading the file twice from Amazon is a bit unnecessary so I want to download it once, convert the image and upload the two images back to Amazon. Pretty straight forward. So, do you think it can be done or do I need to run the convert command twice? One other thing, a docker container does not have a disk so I need to stream the files using stdout.

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-22T05:47:08-07:00
by snibgo
What software reads the stdout that you create? If you write multiple JPGs to a single stream, most software won't be able to separate them, so only the first image is seen.

Can that software read TIFF? If so, then try:

Code: Select all

docker run imagemagick convert http://camendesign.com/code/video_for_everybody/poster.jpg \( -clone 0 -resize 400x400 \) \( -clone 0 -resize 80x80 \) TIFF:-
This writes one TIFF file that contains two images, which most imaging software can read.

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-22T10:38:52-07:00
by maqe
It is my own code that reads the stdout from IM so I can do pretty much what I want. The problem with the JPG output, as you said is where to split the file. TIFF seems to be a different story and I should be able to read one "page" at the time and save both images. Can I still use the some modifers, crop, resize, quality and so on? It's important for me that the images is as sharp as they can be, is TIFF a high quality format that can be converted to true sRGB?

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-22T11:22:33-07:00
by snibgo
JPEG is a low quality format because it is limited to 8 bits/channel/pixel, and compression is lossy (it changes values).

TIFF can use JPEG compression, but gives better quality if used with Zip or LZW or no compression.

Most operations (crop, resize, etc) are independent of the output format.

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-22T14:35:00-07:00
by maqe
Ok,but I guess different quality per image/page is not possible?

Also, is this the only way to achieve saving two images from a single invocation?

Re: convert image to two different sizes and pipe to stdout?

Posted: 2017-06-23T02:21:13-07:00
by snibgo
maqe wrote:Ok,but I guess different quality per image/page is not possible?
What do you mean by "quality"? Each image can be whatever you want it to be.
maqe wrote:Also, is this the only way to achieve saving two images from a single invocation?
There are two ways for "convert" to write images. One is with the filename (or "-") at the end. The other is with "-write" or "+write". A "convert" can use both methods, if you want.