can I convert multiple png images to bmp images in matlab without effect on the quality of the images?
18 views (last 30 days)
Show older comments
I used the following code and the ouput of some images are completely black
d=dir('*.png');
for i=1:length(d)
fname=d(i).name;
png=imread(fname);
fname = [fname(1:end-4),'.bmp'];
imwrite(png,fname,'bmp');
end
0 Comments
Answers (1)
Guillaume
on 19 Feb 2018
Edited: Guillaume
on 19 Feb 2018
That depends on the png images. The png format supports a lot more image formats than bmp. A png image can be up to 16 bits per channel whereas bmp can only go up to 8 bits. I suspect you'd be getting an error when trying to save the image in that case, though.
The png format also supports transparency (which you're not reading, if it exists) whereas bmp doesn't.
However, for a basic 8 bit-per-channel, no transparency png image, there'll be no difference if saved as bmp, other than the bmp using a lot more disk space.
Note that overall png is a much better format than bmp, so unless you have very good reasons, I wouldn't bother with the conversion.
2 Comments
Guillaume
on 20 Feb 2018
The problem in your case is that the png image is indexed but you're not reading the colour map associated with the indices. The following code will work with rgb, greyscale and indexed images:
dircontent = dir('*.png');
for fileidx = 1: numel(dircontent);
filename = dircontent(fileidx).name;
[img, map, trans] = imread(filename);
if ~isempty(trans)
warning('transparency information of %s discarded', filename);
end
[~, basename] = fileparts(filename);
outname = [basename, .bmp'];
if isempty(map)
%greyscale and colour image
imwrite(img, outname, 'bmp');
else
imwrite(img, map, outname, 'bmp');
end
end
Note that this convert an indexed png into an indexed bmp. If your processing code is not designed to cope with indexed image, you may be better off converting indexed images to colour. In that case, replace the above from if isempty(map) by
if ~isempty(map)
img = ind2rgb(img, map);
end
imwrite(img, outname, 'bmp');
end
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!