Saving Image file as a new file.

7 ビュー (過去 30 日間)
David Phung
David Phung 2014 年 10 月 19 日
コメント済み: Image Analyst 2014 年 10 月 20 日
Hi, so my script consists of the user inputting the name of a file, call it picture, and then I would change the RGB values and show it. The final step is to save the image as newpicture.xxx where xxx is the format they inputted. I have tried save, saveas, imsave and many other ways but I havent been successful. Does anyone have any tips?
name= input('Enter name of file: \n', 's'); %file name
format= input ('Enter format of file: \n', 's');%file format
redchange = input('Change in percent of red: \n');%red change
bluechange = input ('Change in percent of blue: \n');%blue change
greenchange = input ('Change in percent of green: \n');%green change
pic= imread(name, format); %reading image
pic= double(pic); %converting to doubles
pic=pic/255; %scaling everything by dividing by 255.
pic(:,:,1)= redchange*pic(:,:,1); %multiply the values by percentage
pic(:,:,2)= greenchange*pic(:,:,2); %multiply the values by percentage
pic(:,:,3)= bluechange*pic(:,:,3); %multiply the values by percentage
pic(pic>1)=1; %makes sure no values are greater than 1
pic(pic<0)=0; %makes sure no values are less than 0
image(pic) %show image
newname=['new' name]; %change name to new+name
newfile=[newname '.jpg'] %add the format to name
axis off %no axis

採用された回答

Jan
Jan 2014 年 10 月 19 日
It is not useful to write only, that your trials haven't been successful. Btter post the code and explain, what goes wrong, e.g. post the error message.
What about imwrite?
  1 件のコメント
David Phung
David Phung 2014 年 10 月 19 日
Wow, that worked. Thank you! I'll post the errors next time. I was trying so many methods and I just ended up deleting them because they did not work.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 10 月 19 日
Why are you asking for the format of the file? It is unnecessary to do that. imread() will figure it out, just like I mentioned in your duplicate posting.
  2 件のコメント
David Phung
David Phung 2014 年 10 月 20 日
編集済み: David Phung 2014 年 10 月 20 日
It's part of a homework assignment. Our teacher wants us to ask the user for the name of the file and the format of the file.
Is there any way I can extract that information out of imread?
Image Analyst
Image Analyst 2014 年 10 月 20 日
No. imread takes the filename as an input. The output of imread() is simply an array and the file format does not apply to array variables, only to data stored on disk in a file.
As long as the file extension in your filename accurately reflects what the format is, there is no reason at all to ask your user for that since, like I said, imread() figures out the file format from the filename extension.
Where the file format would come into play is with imwrite() , not imread() where you had it. imwrite() is where you save the file in the format specified by the user. First of all you don't want to call the variable format since that's the name of a built in command. Call it fileFormat instead. Then you can just say
% Make sure there is no . in the extension they typed in
fileFormat(fileFormat == '.') = [];
% Construct the base file name:
baseFileName = sprintf('newpicture.%s', fileFormat);
% Construct the full file name
folder = pwd; % Or whatever folder you want.
if ~exist(folder, 'dir')
mkdir(folder);
end
fullFileName = fullfile(folder, baseFileName);
% Write out the image in the specified format.
imwrite(pic, fullFileName);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by