Converting a folder of BMP images to true colour JPEG images

5 views (last 30 days)
So I'm using loops to go through each of the images and convert them to JPEGS. Followed by writing my final JPEG images into a second folder for further processing. Initially I have a folder called 'data' which holds the BMP images and when converted to jpeg's initially using matlab they are gray scale. I write these gray scale images into the same folder in my directory called 'data'. I then read the gray scale images back into Matlab and convert them to true colour JPEG's and write them into a new folder called 'data2' in my directory. Below is the code I've been using and I believe I have the right idea but the structure of the code may be wrong..
%If you running the script from the same folder ('autobatch') you can get %the info for each '.jpg' or 'bmp' images as follows
d = dir('.\data\*.jpg');
d2 = dir('.\data\*.bmp');
d3 = dir('.\data2\*.jpg');
for i = 1:length(d)
for i2 = 1:length(d2)
for i3 = 1:length(d3)
BMP = imread(['.\data\','bmp',d2(i2).name]); %Reads in bmp image from folder called 'data' in directory
imwrite(BMP,'.\data\','jpg');
* * *%Converts the initial BMP image to a jpeg image(grayscale) and writes it into the same folder 'data'* * *
A = imread(['.\data\',d(i).name]);
* * *%Reads in new gray scale JPEG from folder 'data'* * *
rgbImage = repmat(A,[1 1 3]);
* * *%Converts the gray scale JPEG to an RGB JPEG* * *
imwrite(rgbImage,['.\data2\',d3(i3).name],'JPEG','Quality',100);
* * *%Writes the finalised image (True colour JPEG) to a new folder in directory called 'data2'* * *
end
end
end

Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!