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
Nour George
Nour George 2020년 6월 25일
@Walter Roberson Thanks a lot

댓글을 달려면 로그인하십시오.

추가 답변 (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