Daniil  Sizov
Daniil Sizov

Reputation: 43

Matlab: how to save TIFF with transparency or PNG with no compression?

I have to process a lot of images and save results to image files with transparency in Matlab. But PNG compression takes too much time for me. How can I save PNG with no compression or TIFF with transparency? Are there other ways to save an image without compression and with transparency?

It's my first question here, sorry for my bad English and wrong question style if there are any mistakes in question.

Upvotes: 4

Views: 5504

Answers (3)

Jonas
Jonas

Reputation: 74940

Using the TIFF class in Matlab you can write TIFFs with transparancy:

%# create a synthetic RGBA image
ch = checkerboard(100);
rgba = repmat(ch,[1,1,4]);
rgba(:,:,4) = rgba(:,:,4)==0;
rgba = uint8(round(rgba*255));

%# create a tiff object
tob = Tiff('test.tif','w');

%# you need to set Photometric before Compression
tob.setTag('Photometric',Tiff.Photometric.RGB)
tob.setTag('Compression',Tiff.Compression.None)

%# tell the program that channel 4 is alpha
tob.setTag('ExtraSamples',Tiff.ExtraSamples.AssociatedAlpha)

%# set additional tags (you may want to use the structure
%# version of this for convenience)
tob.setTag('ImageLength',size(ch,1));
tob.setTag('ImageWidth',size(ch,2));
tob.setTag('BitsPerSample',8);
tob.setTag('RowsPerStrip',16);
tob.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
tob.setTag('Software','MATLAB')
tob.setTag('SamplesPerPixel',4);

%# write and close the file
tob.write(rgba)
tob.close

%# open in Photoshop - see transparency!

Upvotes: 4

Mark Adler
Mark Adler

Reputation: 112512

Matlab's imwrite does not have parameter for the PNG compression level. If it did, you could set it to zero for no compression. While for TIFF it does have a none option for Compression, there is no alpha channel. You can write to the old Sun Raster (RAS) format with an alpha channel and no compression. Though nothing would likely be able to read it.

Upvotes: 1

Aki Suihkonen
Aki Suihkonen

Reputation: 20037

"There is no uncompressed variant of PNG. It is possible to store uncompressed data by using only uncompressed deflate block"

The uncompressed deflate block uses a header of 5 bytes + up to 65535 bytes of uncompressed data per block.

http://www.w3.org/TR/PNG-Rationale.html

Upvotes: 0

Related Questions