Saving Image file as a new file.
조회 수: 1 (최근 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!