Saving Image format problem

I have been collecting images from a camera and i am also collecting the depth information of each frame, I would like to convert the depth frames to

PNG image data, 640 x 480, 16-bit grayscale, non-interlaced

And the Colored images to :

JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, comment: “somecomment”, baseline, precision 8, 640x480, frames 3

I have been looking at the opencv documentation but i am not able to understand how to make this change.

Any help is welcome. Thanks in advance.

show us, what you’ve tried, so far … please

Well nothing mayor, i have tried the usual:

    print("Saving Images to file.")
    color_mat = cv2.resize(color_mat,(640,480))
    depth_mat = cv2.resize(depth_mat,(640,480))

This for the color image i get the following:

color000.jpeg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 640x480, frames 3

and for the depth image its more problematic cuz when i open the png i get a black image not the real image i see.

depth000.png: PNG image data, 640 x 480, 8-bit/color RGB, non-interlaced

I would like to save them in the other formats.
I have seen the command


but not sure how to use it .

for example if i do:

gray_image = cv2.cvtColor(depth_mat, cv2.COLOR_BGR2GRAY)

this is the output:

depth000.png: PNG image data, 640 x 480, 8-bit grayscale, non-interlaced

not sure how to make it 16-bits, still the image is completely black so there most be a way to save a depth image

this displays the image as expected:

cv2.imshow('DepthMap', depth_mat)

well it should already be 16bit, please check depth_mat.type() and depth_mat.channels()

how do you get that depth image ?

if you have an 8bit image for depth, it probably got converted before, please check your code path upwards

1 Like

The image is coming as a numpy.ndarray , then i converted like this:

depth_mat = cv2.cvtColor(np.mat(depth), cv2.COLOR_GRAY2RGB)

So i guess the change should happen here or directly to the np array.
something curious is that depth_mat.type() and depth_mat.channels() wont return

AttributeError: ‘numpy.ndarray’ object has no attribute 'type’

and i am running them after running the cvtColor

But i am able to imshow it.

i remember that in other languages with opencv you have to instantiate the mat with its properties before using it but in python i never had to do that before. i guess it has to be something like that.

that is for C++, where you have cv::Mat (OpenCV)

for python, where OpenCV uses numpy arrays, you check depth_mat.dtype and depth_mat.shape


that makes alot of sense :smiley: The output is :

type=<class 'numpy.ndarray'>
shape=(1, 240, 320, 1)

interesting, looking at the output and the documentation :smiley:

basic ops

I was not able to find information about it, like why my shape had extra number and what differece it makes to have uint8 vs float32

thanks but i still couldnt figure out how to convert it :slight_smile:

okay, you need to present everything you do. literally everything. all the code, but please reduce it to code that actually causes the issue (or is required to run the code), nothing superfluous/incidental.

you are loading those images in some way you haven’t presented. that is vital information. don’t explain in prose. show the things themselves, i.e. source code.