MATLAB: How to write and read compressed GIF files

compressiongifImage Processing Toolboximwritelzw

The function imwrite has parameter 'Compression' like this.
imwrite(data, 'out.tif', 'Compression', 'lzw');
But I want to do this code not in tif files, unfortunately imwrite for compressed GIF files is not supported. Is there any way to do that? And could you explain that step by step? I'm begginer in Matlab.
The compression don't have to be lzw, another loseless compression is alright.

Best Answer

GIF image files are compressed by LZW already. You do not have to trigger this manually. See https://en.wikipedia.org/wiki/GIF.
A short test:
img1 = randi([0,255], 1000, 800, 'uint8'); % Exciting image
img2 = zeros(1000, 800, 'uint8'); % Boring image
imwrite(img1, fullfile(tempdir, 'image1.gif'));
imwrite(img2, fullfile(tempdir, 'image2.gif'));
list = dir(fullfile(tempdir, 'image*.gif'));
list(1)
list(2)
name: 'image1.gif'
bytes: 1100732
name: 'image2.gif'
bytes: 2424
You see: The image containing random data needs much more memory than the one-color image, which can be compressed much better.
Related Question