2

My code below is intended to get a batch of images and convert them to RGB. But I keep getting an error which says to convert to type uint8. I have seen other questions regarding the conversion to uint8, but none directly from an array to uint8. Any advice on how to make that happen is welcome, thank you!

from skimage import io
import numpy as np
import glob, os
from tkinter import Tk
from tkinter.filedialog import askdirectory
import cv2

# wavelength in microns
MWIR = 4.5
R = .692
G = .582
B = .140

rgb_sum = R + G + B;
NRed = R/rgb_sum;
NGreen = G/rgb_sum;
NBlue = B/rgb_sum;

path = askdirectory(title='Select PNG Folder') # shows dialog box and return the path
outpath = askdirectory(title='Select SAVE Folder') 
for file in os.listdir(path):
    if file.endswith(".png"):
        imIn = io.imread(os.path.join(path, file))
        imOut = np.zeros(imIn.shape)    

        for i in range(imIn.shape[0]): # Assuming Rayleigh-Jeans law
            for j in range(imIn.shape[1]):
                imOut[i,j,0] = imIn[i,j,0]/((NRed/MWIR)**4)
                imOut[i,j,1] = imIn[i,j,0]/((NGreen/MWIR)**4)
                imOut[i,j,2] = imIn[i,j,0]/((NBlue/MWIR)**4)
        io.imsave(os.path.join(outpath, file) + '_RGB.png', imOut)

the code I am trying to integrate into my own (found in another thread, used to convert type to uint8) is:

info = np.iinfo(data.dtype) # Get the information of the incoming image type
data = data.astype(np.float64) / info.max # normalize the data to 0 - 1
data = 255 * data # Now scale by 255
img = data.astype(np.uint8)
cv2.imshow("Window", img)

thank you!

3
  • What is data and its type in the last code block? Commented Apr 22, 2020 at 17:01
  • I do not know, the last codeblock was added from another post on SO. I believe data was just an image file? maybe a jpeg/png type. Commented Apr 22, 2020 at 17:10
  • you must first know what is the time of imgIn, it should be UInt8 normally. Second, why is there an exponent 4 in your normalisation? Can you try: io.imsave(os.path.join(outpath, file) + '_RGB.png', imOut.astype(np.uint8))? Commented Apr 22, 2020 at 17:16

1 Answer 1

2

Normally imInt is of type uint8, after your normalisation it is of type float32 because of the casting cause by the division. you must convert back to uint8 before saving to PNG file:

io.imsave(os.path.join(outpath, file) + '_RGB.png', imOut.astype(np.uint8))

Note that the two loops are not necessary, you can use numpy vector operations instead:

MWIR = 4.5

R = .692
G = .582
B = .140

vector = [R, G, B]
vector = vector / vector.sum()
vector = vector / MWIR
vector = np.pow(vector, 4)

for file in os.listdir(path):
  if file.endswith((".png"):
    imgIn = ...
    imgOut = imgIn * vector
    io.imsave(
      os.path.join(outpath, file) + '_RGB.png', 
      imgOut.astype(np.uint8))
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your response! In your solution code, because the loops have been deleted there is no inpath or outpath for saving. Where would you recommend I put these as they are needed to get the directory of files. Thank you again!
Only the two inner loops have been deleted, you can scope this in your file reading loop of course ! The vector part should be outside (only computed once) though.
I'm sorry I think I'm getting a bit confused. Would it be terribly inconvenient to show me what you mean by editing your code so that it is integrated into my original code, as you are describing? If not, I really appreciate your help so far and will figure it out! thank you!
Got it! It runs and I've debugged any small issues that have popped up. For some reason the images are saving as all-black, so I may need to post another question separately to deal with that. Thank you for your help!
Yes try tu debug line per line. Black image could be that image was not uint8 before, try to multiply by 255

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.