0

This may have been asked before, but I couldn't find it!

I need to process a series of image files with jimp, and I'm trying to be as efficient as possible.

My current program simply uses "await jimp.read(...)" synchronously before processing it. What I'd like to do is read the images asynchronously, but here's the catch: I need to process the results in order.

At a minimum, I'm thinking I need to read the next image asynchronously while processing the current image, then (when finished processing) wait for the next image to finish being read before starting the next iteration, if that makes sense.

Is there a pattern I should use for doing this? Is there a better approach than what I describe above?

Thanks!

Edited to include a summary of my current approach:

image = await jimp.read("image_1");

for (let i = 1; i <= totalImages; i++) {
    if (i < totalImages)
        var imagePromise = jimp.read("image_" + (i+1));
    processImage(image);
    image = await imagePromise;
}

It just processes one image while reading the next, but it may need to (a)wait if the next image hasn't been completely read yet.

ScottyB
  • 2,167
  • 1
  • 30
  • 46
  • 1
    For a good solution, please explain more precisely what can happen in parallel and what not. For example, is it allowed to read _all_ images in parallel and then process them in order? What about _writing_ the processed images? And, perhaps also, _why_ must certain steps be carried out in order? Could the output of one processing step be the input for another? – Heiko Theißen Feb 05 '23 at 07:34
  • Thanks @HeikoTheißen. Sorry for the delay. Yes, all images could be read in parallel, but they need to be processed in order. For each of a selected set of pixels across all images, it writes an analysis (not an image) at the end. The output of one step is input for the next step. I came up with a pattern that works, and I'll put it at the end of the original question. – ScottyB Feb 11 '23 at 23:48
  • Have you checked [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)? – Kostas Minaidis Feb 12 '23 at 00:10

0 Answers0