Converting the code with repeated instructions to a simpler code

1 回表示 (過去 30 日間)
Nour George
Nour George 2020 年 6 月 25 日
コメント済み: Nour George 2020 年 6 月 25 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 25 日
plot(Qualities, CompressedRatios)
Nour George
Nour George 2020 年 6 月 25 日
@Walter Roberson Thanks a lot

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by