convert image with (.png) format to (.jpg) format

2019-09-14 22:55发布

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 all 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

3条回答
爷的心禁止访问
2楼-- · 2019-09-14 23:28

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);   
查看更多
仙女界的扛把子
3楼-- · 2019-09-14 23:31

What you need to do is this:

  1. Get a list of all the files that you want to convert. Use the function dir, which returns a structure with a component name.
  2. 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.
  3. 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.

查看更多
SAY GOODBYE
4楼-- · 2019-09-14 23:34

This here, combined with what you have should do the trick!

查看更多
登录 后发表回答