tags:

views:

52

answers:

2

Hi All,

How do I save jpeg images with no compression in Matlab?

I tried

targetImageFile = 'skype2.png';
targetImage = imread(targetImageFile);

imwrite(targetImage,'output.png','Compression','none',...
       'WriteMode','append');

However, I got

input:
compressionRatio      = 1.992735e+000
output:
compressionRatio      = 2.090858e+000

Thank you for your advice.

+3  A: 

It's unclear whether you are trying to save your image in JPEG or PNG format (your question and code sample each use something different), but if you look at the documentation for IMWRITE you will notice that neither of those formats uses a 'Compression' or 'WriteMode' parameter. The TIFF and HDF4 formats use those two parameters.

For JPEG format, you can adjust the 'Mode' or 'Quality' properties to reduce compression of the image. PNG format uses lossless compression. If you want to avoid all compression (lossy or lossless), you may as well just save your image in BMP format.

Here are a few examples of saving an image in different formats and the resulting file size of the output image:

X = imread('peppers.png');             %# Sample image: 589,824 bytes of data
imwrite(X,'peppers.bmp');             %# Bitmap output: 589,878 byte output file
imwrite(X,'peppers.png');     %# PNG output (lossless): 287,589 byte output file
imwrite(X,'peppers.jpg');       %# JPEG output (lossy):  23,509 byte output file
imwrite(X,'peppers.jpg',...     %# JPEG output (lossy): 144,068 byte output file
          'Quality',100);
imwrite(X,'peppers.jpg',...  %# JPEG output (lossless): 306,061 byte output file
          'Mode','lossless');
gnovice
+4  A: 

If you want no compression, then surely you want a bitmap ('bmp')? If you want lossless compression, then you want 'mode' to be 'lossless' for 'jpg'. 'png' is already a lossless format.

'Compression' is only an option for the 'tiff' format.

See, http://www.mathworks.com/help/techdoc/ref/imwrite.html#f25-713936

James