Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list of several hundred images, and I want to average the list to get a single image. Each image is 12bit TIFF with 2048*2048 pixels, and I am using the following to do the job:


The problem is that the data becomes huge. I tried to do the task with about 400 images with 2048*2048 resolution. The computer ran out of ram, created a 25GB of virtual memory, and could not finish the task.

Is there any other alternative that I can use to do the task?

I would greatly appreciate your help.

share|improve this question

migrated from Mar 23 '13 at 22:09

This question came from our site for professional and enthusiast programmers.

Here is a way where you hold only two images in memory at any time and the only memory increase when you add up more images is for holding the file names.

Using @stuartw "sample image list generator" :)

(* generate a sample image list*)
img = Import[""];
path = Export[ToFileName[$UserDocumentsDirectory, "hilbert.jpg"], img]; 
set = Table[path, {30}];

(* Now process it *)
Image[Fold[#1 + ImageData@Import@#2 &, 0 set[[1]], set]/Length@set]
share|improve this answer
Why 0 set[[1]] and not just 0 ? – Simon Woods Feb 25 '14 at 22:18
@SimonWoods It's an old fixation of mine. Useful when the folding list has "structure" – Dr. belisarius Feb 25 '14 at 22:20

By rearranging a few things, you can process one image at a time. I did not test this with large images, but it should help

img = Import[""];
path = Export[ToFileName[{NotebookDirectory[]}, "hilbert.jpg"], img];

(*{100, 125}*)

imageList = Table[path, {30}];

sumImageData = Table[{0, 0, 0}, {125}, {100}];
nImages = 0;
processData[{sumArray_, n_}, path_] := Module[{},
    Return[{sumArray + ImageData[Import[path]], n + 1}]

({sumImageData, nImages} = 
  processData[{sumImageData, nImages}, #]) & /@ imageList;


(*{125, 100, 3}
30 *)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.