For calculating compressed image file size before & after compression, do I have to save images?Or calculating matrix bytes is enough and correct?
6 views (last 30 days)
Show older comments
Hi friends! I want to calculate and compare image file size before and after the operation. As far as I know, image file size info can be get from imfinfo but I have image matrix only.
- Do I have to save the image to get file size?
- Does saving with different extensions make affect image file size (.jpg, .bmp, .png ...etc.) ?
- How can I learn image file size without saving? ( Can I achieve this matrix calculations row*size*dimension or etc.)
Thank you in advance!
4 Comments
Accepted Answer
Image Analyst
on 28 Jun 2020
I'm not sure the matrix size in MATLAB will be useful, though it might be interesting. You could use it to figure out how big the headers are for the uncompressed formats like BMP and TIF. What you want to do it use dir() or imfinfo() to get the size on disk, which is usually what people mean when they talk about compression. So just use create different filenames with different extensions representing the format you want to write the image out in, and then imwrite() to write it to disk. Then use dir() to get back the number of bytes on disk. For example
rgbImage = imread('peppers.png');
whos rgbImage
filename = 'image jpg.jpg';
imwrite(rgbImage, filename);
filename = 'image bmp.bmp';
imwrite(rgbImage, filename);
filename = 'image tif.tif';
imwrite(rgbImage, filename);
d = dir('image *.*')
for k = 1 : length(d)
fprintf('%s is %d bytes on disk.\n', d(k).name, d(k).bytes);
end
You'll see:
Name Size Bytes Class Attributes
rgbImage 384x512x3 589824 uint8
d =
3×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
image bmp.bmp is 589878 bytes on disk.
image jpg.jpg is 23509 bytes on disk.
image tif.tif is 593776 bytes on disk.
You can see the header size for BMP is 589878 - 589824 = 54 bytes. Of course that doesn't work for compressed formats like PNG and JPG. By the way, don't save images in a lossy format like JPG if you plan on using them for image analysis.
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!