Saving Image file as a new file.

4 views (last 30 days)
David Phung
David Phung on 19 Oct 2014
Commented: Image Analyst on 20 Oct 2014
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

Accepted Answer

Jan
Jan on 19 Oct 2014
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 Comment
David Phung
David Phung on 19 Oct 2014
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.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 19 Oct 2014
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 Comments
David Phung
David Phung on 20 Oct 2014
Edited: David Phung on 20 Oct 2014
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 on 20 Oct 2014
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);

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!