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):
在我的應用程序中,我需要將所有的圖像(.png)格式轉換為(.jpg)格式。我使用了imwrite函數(Matlab的一個函數):
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?
我可以只轉換一個圖像…我需要轉換所有圖像並將它們保存在一個新的文件夾中。有人能告訴我怎么做嗎?在將圖像轉換為(.jpg)格式后,圖像的屬性是否有任何變化?
Please forward your valuable suggestions.
請提出寶貴意見。
Thanks
謝謝
3 个解决方案
#1
3
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 component name.
獲取要轉換的所有文件的列表。使用函數dir,它返回一個帶有組件名稱的結構。
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').
當您編寫轉換后的文件時,將相對路徑包含在您正在編寫的文件的名稱中,比如: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.
轉換后圖像的屬性是否有變化?一般來說,是的,因為png和jpeg編碼的信息有差異。Matlab幫助函數imread和imwrite解釋了這一點。您可能會發現,您需要或想要修改您在編寫之前閱讀的圖像。
#2
1
This here, combined with what you have should do the trick!
這里,再加上你應該做的事情!
#3
-4
I take the suggestion from members and I tried the following coding so I was able to convert the (.png ) format to (.jpg) format:
我采納了成員的建議,我嘗試了下面的編碼,所以我能夠轉換(。png格式至(.jpg)格式:
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);