Converting the code with repeated instructions to a simpler code

1 次查看(过去 30 天)
This code is not simple as there are many repeated instructions and the difference is the value of the quality, so I want the same code, but it is simpler without repetition. How can i do this by "for loop"
and How can i use plot Here ?
figure
q=[0 5 15 25 50 75 90 100]
Original=imread('lena (1).jpg');
information = imfinfo('lena (1).jpg');
OriginalFileSize=(information.Width*information.Height*information.BitDepth)/8
%%for Quality 1
imwrite(Original,'CompressedQuality1.jpg','jpg','Quality',0) ;
information = imfinfo('CompressedQuality1.jpg');
CompressedFileSize1=information.FileSize;
CompressedRatio1=OriginalFileSize/CompressedFileSize1
%%for Quality 5
imwrite(Original,'CompressedQuality2.jpg','jpg','Quality',5);
information = imfinfo('CompressedQuality2.jpg');
CompressedFileSize2=information.FileSize;
CompressedRatio2=OriginalFileSize/CompressedFileSize2
%%for Quality 15
imwrite(Original,'CompressedQuality3.jpg','jpg','Quality',15);
information = imfinfo('CompressedQuality3.jpg');
CompressedFileSize3=information.FileSize;
CompressedRatio3=OriginalFileSize/CompressedFileSize3
%%for Qualoty 25
imwrite(Original,'CompressedQuality4.jpg','jpg','Quality',25);
information = imfinfo('CompressedQuality4.jpg');
CompressedFileSize4=information.FileSize;
CompressedRatio4=OriginalFileSize/CompressedFileSize4
%%for Quality 50
imwrite(Original,'CompressedQuality5.jpg','jpg','Quality',50);
information = imfinfo('CompressedQuality5.jpg');
CompressedFileSize5=information.FileSize;
CompressedRatio5=OriginalFileSize/CompressedFileSize5
%%for Quality 75
imwrite(Original,'CompressedQuality6.jpg','jpg','Quality',75);
information = imfinfo('CompressedQuality6.jpg');
CompressedFileSize6=information.FileSize;
CompressedRatio6=OriginalFileSize/CompressedFileSize6
%%for Quality 90
imwrite(Original,'CompressedQuality7.jpg','jpg','Quality',90);
information = imfinfo('CompressedQuality7.jpg');
CompressedFileSize7=information.FileSize;
CompressedRatio7=OriginalFileSize/CompressedFileSize7
%%for Quality 100
imwrite(Original,'CompressedQuality8.jpg','jpg','Quality',100);
information = imfinfo('CompressedQuality8.jpg');
CompressedFileSize8=information.FileSize;
CompressedRatio8=OriginalFileSize/CompressedFileSize8

采纳的回答

Walter Roberson
Walter Roberson 2020-6-25
Qualities = [0 5 15 25 50 75 90 100];
for qidx = 1 : length(Qualities)
filename = sprintf('CompressedQuality%d.jpg', qidx);
imwrite(Original, filename, 'jpg', 'Quality', Qualities(qidx));
information = imfinfo(filename);
CompressedFileSizes(qidx) = information.FileSize;
CompressedRatios(qidx) = OriginalFIleSize / CompressedFileSizes(qidx);
end
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by