pretzlstyle
pretzlstyle

Reputation: 2962

How to create an image with any arbitrary number of channels? (Python)

I am working on a project in Python, Anaconda distro, and need to create and process images that have any arbitrary number of channels. For example, RGB has three channels

(R,G,B)

and I need to have any amount of user-defined channels, which could as many as 90 or so (which are actually elemental channels, from x-ray fluorescence data):

(Fe, P, K, S, Si, ...)

where each channel is simply a grayscale image (obviously).

So far I have been using the PIL Image module, and it works quite well except for this particular task. PIL does not support used defined image modes, and only has a select few modes to choose from, none of which go above 4 channels (CMYK).

The documentation notes this very briefly, saying:

However, PIL doesn’t support user-defined modes; if you to handle band combinations that are not listed above, use a sequence of Image objects.

This seems vague, and I'm not sure I would know exactly how to implement this. A sequence of Image objects? Meaning a sequence of objects, each of which do have a PIL supported image mode? Seems inefficient and ugly.

How can I accomplish this? Is there a package I can use other than PIL?

If it really comes down to it, I don't need to render an image, I can just use the "pixel" data for processing, but in the end it would still be nice to have an image as a result.

Upvotes: 3

Views: 988

Answers (2)

mrk
mrk

Reputation: 10366

With numpy you can store Images with multiple channels (here shown for 3 channels):

import numpy as np
num_channels = 3
random_image = np.random.randint(0,256, (300,400,num_channels))

Upvotes: 0

pplonski
pplonski

Reputation: 5839

Maybe simple numpy arrays will be sufficient for you?

Upvotes: 1

Related Questions