Conversion of .BMP to .jpg images
显示 更早的评论
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
采纳的回答
更多回答(1 个)
matt dash
2015-2-16
0 个投票
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 个评论
Sean
2015-2-16
Sean
2015-2-16
Eric
2015-2-16
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
2015-2-16
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
2015-2-17
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
类别
在 帮助中心 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!