Conversion of .BMP to .jpg images
Show older comments
Im using the code below to convert .bmp images to .jpeg, Once typed into the command window no errors appear. However there are no new images written into the 'JPEG Data' folder, Can anyone help?
%load images from file
filePathIn = '.\Project\Matlab Folder\BMP Data';
filePathOut = '.\Project\Matlab Folder\JPEG Data';
%Load names of .bmp files in folder filePathIn
d = dir([filePathIn,'*.bmp']);
%for each .bmp file in the directory, convert to jpg
for i = 1:length(d)
%read .bmp file
fname = d(i).name;
%BMP = imread([filePathIn,d(i).name]);
BMP = imread([filePathIn,fname]);
%convert to jpg and rename with .jpg extension
fname = [fname(1:end-4),'.jpg'];
imwrite(BMP,[filePathIn,fname],'jpg');
%reload this file in .jpg format
A = imread([filePathIn,fname]);
rgbImage = repmat(A,[1 1 3]);
%write jpg image to new folder
imwrite(rgbImage,[filePathOut,fname],'JPEG','Quality',100);
end
Accepted Answer
More Answers (1)
matt dash
on 16 Feb 2015
0 votes
Looks like you're missing a path separator between your folder name and file name. You should use the fullfile fucntion to combine paths with filenames instead of trying to do it with [].
6 Comments
Sean
on 16 Feb 2015
Sean
on 16 Feb 2015
Eric
on 16 Feb 2015
Can you use copyfile to generate the second image rather than writing it out a second time? This seems more efficient. More importantly, if this fails then the problem is not in how you're calling imwrite and so on, but perhaps is an indication of access problems.
Image Analyst
on 16 Feb 2015
No you can't. Copying a file and giving it a new extension only changes the filename, not the data in the file. You have to call imread(), then imwrite() with a new filename. You do not need to call them twice like the code above is showing.
Eric
on 17 Feb 2015
That's what I was getting at in my comment above. I didn't mean copy the BMP file and simply rename it to JPG, but to copy the first jpeg (the one that wrote correctly) to the location where the write failed.
You could check two things:
1. Does the output directory exist when you try to write to it?
2. Do you have write privileges to the directory?
-Eric
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!