How can I turn distinct R G B arrays back into an image?

62 Views Asked by At

In Python:

I read in an image and I want to process each channel (RGB) separately, then put the modified channels back together into an image and save it to disk. I have tried everything I can think of, but I get mangled images back no matter what.

BTW, I can't use Pillow for most of this because I want 16 bits to process.

I've tried innumerable variations of this:

import imageio
import numpy as np
from PIL import Image

def load_pic():
    global picky, reds, greens, blues, shape
    filename = "FinalStacked.fit"
    print(f"loading {filename}")
    picky = imageio.v3.imread(filename, plugin = "FITS")
    shape = picky.shape
    print(f"Image loaded, size = {shape}")

    reds = picky[0]
    greens = picky[1]
    blues = picky[2]
    print("picture loaded")

def finish():

    out = np.dstack((reds, greens, blues))
    
    im = Image.fromarray(out, 'RGB')
    print(im.mode)
    imageio.v3.imwrite("done.png", im)

load_pic()
finish()

But nothing seems to work. I would expect the above to return a reasonable facsimile of the input image, but it doesn't.

1

There are 1 best solutions below

6
Suraj Shourie On

As mentioned in the comments you are not actually getting the RGB channels with your code. Something like the below code will work for you:

from PIL import Image
import numpy as mp 

# read image get RGB
picky = imageio.v3.imread(filename)

# create image
Image.fromarray(picky, 'RGB')

The error in your code is that you're taking the wrong channels

# UPDATETHIS
reds = picky[:,:,0,]
greens = picky[:,:,1]
blues = picky[:,:,2]

out = np.dstack((reds, greens, blues))
print(picky.shape, out.shape)

Print:

((534, 800, 3), (534, 800, 3))