convert image to two different sizes and pipe to stdout?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
maqe
Posts: 17
Joined: 2017-06-18T02:21:18-07:00
Authentication code: 1151

convert image to two different sizes and pipe to stdout?

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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:
snibgo's IM pages: im.snibgo.com
maqe
Posts: 17
Joined: 2017-06-18T02:21:18-07:00
Authentication code: 1151

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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:
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
maqe
Posts: 17
Joined: 2017-06-18T02:21:18-07:00
Authentication code: 1151

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Sorry, that is something I cannot answer.
maqe
Posts: 17
Joined: 2017-06-18T02:21:18-07:00
Authentication code: 1151

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
maqe
Posts: 17
Joined: 2017-06-18T02:21:18-07:00
Authentication code: 1151

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
maqe
Posts: 17
Joined: 2017-06-18T02:21:18-07:00
Authentication code: 1151

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
maqe
Posts: 17
Joined: 2017-06-18T02:21:18-07:00
Authentication code: 1151

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply