convert image with (.png) format to (.jpg) format in Matlab
In my application I need to convert all images with (.png) format to (.jpg) format. I used the imwrite
function (one of Matlab's functions):
S=imread('D-1.png');
imwrite(S,'D-1.jpg');
and I can convert just one image... I need to convert al开发者_如何学Cl images and save them in a new folder. Could any one please let me know how I can do that? Is there are any changes in the properties of the image after convert it to the (.jpg) format?
Please forward your valuable suggestions.
Thanks
What you need to do is this:
- Get a list of all the files that you want to convert. Use the function
dir
, which returns a structure with a componentname
. - Write a loop to go through the files one at a time and convert them -- you can use the code you've already written, but of course you'll have to set the file names at each iteration.
- When you write the converted file out, include the relative path to the new folder in the name of the file you are writing, something like:
imwrite(S,'./newfolder/D-1.jpg')
.
Are there any changes to the properties of the image after conversion ? In general yes, since there are differences in the information that png and jpeg encode. The Matlab help for functions imread
and imwrite
explain some of this. You may find that you need, or want, to modify the image that you read before writing it.
This here, combined with what you have should do the trick!
I take the suggestion from members and I tried the following coding so I was able to convert the (.png
) format to (.jpg
) format:
fileFolder = fullfile('D:','\Last Work Nov. 2010 16','Last ColTexFeapro28 Nov', 'Brodatz classes', 'Brodatz999');
dirOutput = dir(fullfile(fileFolder,'D*.png'));
fileNames = {dirOutput.name};
for k=1:length(fileNames)
I=fileNames{k};
S=imread(I);
newName = sprintf('image%04d.jpg',k); // convert from (.png to .jpg ) format
movefile(fileNames{k},newName);
精彩评论