用Maltab將jpg圖檔儲存成png無失真圖檔
用Maltab將jpg圖檔儲存成png無失真(lossless)圖檔,只需要一個只令呦!
imwrite(img, filepath, 'Compression', 'none');
下面給一個處裡一整個資料夾的範例,還會check是否與原圖一模一樣。
DoLosslessValidation = true;
files=dir('*.jpg');
if size(files,1) <= 0
error('Cannot find any *.jpg files!');
end
cnt = 0;
valid_cnt = 0;
for i=1:size(files,1)
[pathstr, name, ext] = fileparts(files(i).name);
img = imread(files(i).name);
if ~isempty(img)
imwrite(img, [name '_lossless.png'], 'Compression', 'none');
cnt = cnt+1;
if DoLosslessValidation
rec_img = imread([name '_lossless.png']);
islossless = 0==sum(double(rec_img(:))-double(img(:)));
if islossless
lossless_msg = 'lossless';
valid_cnt = valid_cnt+1;
else
lossless_msg = 'lossy';
end
disp(['converted image ..... ' int2str(cnt) ' (' lossless_msg ')']);
else
disp(['converted image ..... ' int2str(cnt)]);
end % DoLosslessValidation
end
end
disp(['Total Processed Images:' Int2str(cnt)]);
if (DoLosslessValidation)
disp(['Lossless valid counter = ' num2str(valid_cnt/cnt*100) '%']);
end
留言