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!