Home > database >  How do I convert np.uint8 into a format usable by PIL.Image?
How do I convert np.uint8 into a format usable by PIL.Image?

Time:11-06

I am trying to combine my computer vision code with existing code from DepthAI(this is the code that actually retrieves each frame from the camera) in order to analyze a depth map image to determine obstacles. The issue I am running into is that each frame of video from the DepthAI code seems to be in the form of 'np.uint8'. My computer vision code uses PIL.Image, and I was testing it previously using only .png images. Now that I am integrating the code together, I can't seem to get the DepthAI-retrieved-frames to work with the PIL.Image functions. The code boxed in # tries to reshape the np.uint8 array into a common image resolution (1024x750) but the output image is nonsense. I just want to analyze the colormap image so I can pass this class and graduate :( Does anyone know how to go from np.uint8 to a format compatible with Image?

import cv2
import depthai as dai
import numpy as np
from PIL import Image


    q = device.getOutputQueue(name="disparity", maxSize=4, blocking=False)

    while True:
        inDisparity = q.get()  # blocking call, will wait until a new data has arrived
        frame = inDisparity.getFrame()
        # Normalization for better visualization
        frame = (frame * (255 / depth.initialConfig.getMaxDisparity())).astype(np.uint8) #this is where the frame is made into np.uint8

        cv2.imshow("disparity", frame)                  # the resulting images from disparity look correct

        frame = cv2.applyColorMap(frame, cv2.COLORMAP_JET)
        cv2.imshow("disparity_color", frame).       # the resulting image from the colormap looks correct

        #now trying to get the frame to work with PIL.Image
 #####################################################
        reshapedFrame = np.reshape(frame, (750,1024))#
        im = Image.fromarray(reshapedFrame)          #  
        im.show()                                    #
######################################################

        if cv2.waitKey(1) == ord('q'):
            break

cv2.show() output of disparity and corresponding color map

output from reshaped np.uint, nonsense!

CodePudding user response:

Specify the grayscale mode in fromarray:

im = Image.fromarray(reshapedFrame, 'L')

CodePudding user response:

Deleting all the code in the box and replacing with pil_image = Image.fromarray(np.uint8(frame)) solved the issue. Just had to specify the kind of data that was in the array I reckon.

  • Related