Saving Image file as a new file.
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个)
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 个评论
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 Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!