convert 30000 images using 'convert' instead of 'mogrify'?

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?".
Post Reply
jun

convert 30000 images using 'convert' instead of 'mogrify'?

Post by jun »

Hello,

This must be something very simple and obvious, but I have wasted a whole day today. I have thousands of timeseries images, which I need to convert their format. Normally, I would have done the following:

mogrify *.tif -separate -background black -compose plus -flatten *.tif

However, my version of mogrify does not recognize '-compose' option (sigh), and I am using ImageMagick 6.4.8-9 (latest).

The above works fine with 'convert' for a single image, eg,

convert img0001.tif -separate -background black -compose plus -flatten img0001_conv.tif

How do I use 'convert' to repeat the same task for many images? I have spent a lot of time searching this forum, but no answers found so far.

Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert 30000 images using 'convert' instead of 'mogrify'?

Post by fmw42 »

jun wrote:Hello,

This must be something very simple and obvious, but I have wasted a whole day today. I have thousands of timeseries images, which I need to convert their format. Normally, I would have done the following:

mogrify *.tif -separate -background black -compose plus -flatten *.tif

However, my version of mogrify does not recognize '-compose' option (sigh), and I am using ImageMagick 6.4.8-9 (latest).

The above works fine with 'convert' for a single image, eg,

convert img0001.tif -separate -background black -compose plus -flatten img0001_conv.tif

How do I use 'convert' to repeat the same task for many images? I have spent a lot of time searching this forum, but no answers found so far.

Thank you.
You probably need to write a script to loop through your images and use convert

Perhaps someone else can suggest another option for mogrify. I tried -separate -fx "u[0]+u[1]+u[2]", but mogrify did not like -fx either.

Perhaps you can explain why you want to add the channels together rather than average them as adding them will result in getting a lot of white due to clipping.
jun

Re: convert 30000 images using 'convert' instead of 'mogrify'?

Post by jun »

Thanks for your comments. Here's my response.

1. I assume there are many others who have encountered similar problems, and I was hoping to find a script here and was surprised by that. My filenames are structured (numbered), so it's relatively straightforward to write one in this case, but life would have been a lot easier if simple wildcards had worked. Has anyone else faced similar problems?

2. I have read from many posts that '-fx' is a very slow option (myself confirmed).

3. I thought that by adding the channels I won't lose information, whereas average could cause round-off errors. The max intensity of each channel is far below 32768 (16bit channels). In fact, our sw mistakenly split the original grayscale images by adding a fake color (yellow), and I am just trying to "recover" the original images.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert 30000 images using 'convert' instead of 'mogrify'?

Post by anthony »

jun wrote: mogrify *.tif -separate -background black -compose plus -flatten *.tif

However, my version of mogrify does not recognize '-compose' option (sigh), and I am using ImageMagick 6.4.8-9 (latest).
First you should remove the first '*.tif' from mogrify. It is incorrect usage.

Second, not mogrify can NOT normally handle multi-image sequences, as it is designed to loop though one image at a time.

You will need to use a convert in a looped command.
Examples of this in in IM Examples, Basics, Mogrify,
Batch processing Alternatives...
http://www.imagemagick.org/Usage/basics/#mogrify_not

Now as for what you are trying to do. Why do you want to 'add' all the images together? Perhaps if you told us what you actually want to do we may be able to give you a better suggestion, or pointer to a solution.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jun

Re: convert 30000 images using 'convert' instead of 'mogrify'?

Post by jun »

Now as for what you are trying to do. Why do you want to 'add' all the images together? Perhaps if you told us what you actually want to do we may be able to give you a better suggestion, or pointer to a solution.
Thanks for pointing out the web page that shows examples of using 'convert' in a loop for processing multiple images. A friend wrote a perl script for me, though I would like to ask whether implementing wildcards are disadvantageous or have potential confusions. Naively, I would think that it'd be extremely convenient.

To answer your question, let me elaborate the real situation here. We work in a lab and we take images of cells using high-sensitive cameras. For compatibility, we store images in 16bit unsigned TIFF. Typically, we take 10-20 pictures per minute and we continue for several days. So after one experiment, we generate order of 10,000 images.

Importantly, we only record the intensity of the pixels, thus by definition our images are all grayscale. Now, some software use fake colors for visualization on the screen. Say, your E.coli cells are 'yellow' because of the specific yellow wavelength of the proteins emitting the light. Internally, grayscale is splitted into three channels depending on the choice of the fake color. NB. what we need is the original grayscale image, not the fake color ones.

Unfortunately, our image acquisition software is a bit buggy, and sometimes it "decides" to record the images with the fake color! In this particular incidence, the colored TIFF images contain 3 channels, where R and G share roughly 3:7 intensities, and B zero. I do not know if our software normalized the total intensity. Anyway, by doing this, I am afraid the software has already lost some information, hopefully, not terribly. This is why I wanted to add all the channels, to restore our original grayscale (intensity) information. This should prevent further information loss. Fortunately, the max intensity even after the addition is still significantly below what 16bit TIFF allows (say, 2^16), so I decided to go for this.

Having elaborated above, I would very much appreciate if you have better solutions or insights into this problem.

Thank you.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert 30000 images using 'convert' instead of 'mogrify'?

Post by anthony »

I missing the -separate otherwise I would have realised what you were doing.

HOWEVER in a IM Q16, reading a 8 bit image will promote the colors to 16 bit!
As such I would still be carefull about just adding the values!

You may look to look at the 'grayscaling techniques' shown on...
http://www.imagemagick.org/Usage/color/#grayscale
However any FX solution is NOT recomended as they are slow.
Basically it is an operator of last resort and for development for new operations.

The -evaluate method in the above section can be used to scale the channel values appropriately before adding them together. better still the -recolor method will do it all in one operation, just make each of the three lines the same set weighted arguments to generate grayscale.

-recolor should be available in "mogrify" (report a bug if it isn't) as it does not deal with multiple images.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply