How to compress 32 bit bmp images to 8 bit bmp?

I have thousands of 32 bit grayscale bmp images that I want to compress to 8 bit bmp grayscale images using a script to reduce the file size and save space while maintaining the image quality. I have looked at using the "im2uint8()", "rgb2ind()", and "imwrite()" functions to try and achieve this, but I either get fully black images or, when it compresses images that maintain the image quality, they turn out to be 24 bit and not 8 bit. I have two scripts I've tried posted below this. Any ideas why it isn't compressing to 8 bit? Also, is there a reason why it only compresses to 24 bit? Thank you to any replys.
Here are a couple scripts I've tried:
im = imread(input_filename);
map = gray(256);
imwrite(im,map,output_filename);
This results in a 24 bit image.
Next:
im = imread(input_filename);
im8 = im2uint8(im);
imwrite(im8,output_filename);
This results in a completely black image.

 Accepted Answer

you can stretch and compress it on your own, just as you want
im=imread('new.png');
im=rgb2gray(im);
class(im)
ans = 'uint8'
imshow(im);
im16=uint16(im)*2^8; % 16-8=8
imshow(im16)
im32=uint32(im)*2^24; % 32-8=24
imshow(im32)
% and similarly downwards
clearvars -except im32;
im16=uint16(im32/2^16);
imshow(im16)
im8=uint8(im16/2^8);
imshow(im8)

4 Comments

I tried to change it and then save like the example you showed, but it saved as a 24 bit image again. I don't understand. The image originally reads in with imread as a uint8, but then doesn't save as an 8 bit image.
Here is the script I tried:
im = imread(input_filename);
im32=uint32(im)*2^24; % 32-8=24
im16=uint16(im32/2^16);
im8=uint8(im16/2^8);
imwrite(im8,output_filename);
first, you dont need each single step of compression, it should only show the principle
% if im is a grayscale image with 32 bit per pixel
im8=uint8(im/2^(32-8)); % from 32 bit to 8 bit
what is you output_filename? It sounds like the 8 bit grayscale image is saved is RGB image, which has 8 bit per pixel per channel, resulting into 24 bits per pixel
can you provide an exemplary 32 bit image please. Maybe our grayscale appears gray but is an RGB image. e.g. add an additional rgb2gray( ) before saving. But I can't tell you more without having an exemplary image
Thank you very much for suggesting I check the image for what color type was. I checked the image info and found the image as "True color". All my images were captured with no color, but the images still rgb and not grayscale, so using the rgb2gray() was required to make it work.
Here is the functional script:
im = imread(input_filename);
im8gray = rgb2gray(im);
imwrite(im8gray,output_filename);
btw, your initial rgb image is not 32 bit, but already 24bit (3x8 bit). so it is already uint8, that's why you do not need those divisions described above
im=imread('RefImage6.bmp');
size(im,3)
ans = 3
class(im)
ans = 'uint8'
im=rgb2gray(im);
class(im)
ans = 'uint8'

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Asked:

on 8 Feb 2023

Commented:

on 9 Feb 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!