MATLAB Answers

imwrite lossless jpg filesize

4 views (last 30 days)
Nick on 12 Jul 2021
Answered: Vineet Joshi on 19 Jul 2021
I am reading, cropping, and writing bulk DSLR jpg images in Matlab using imread, imcrop, and imwrite in a loop but have two questions:
-I want to retain the original pixel value information so I am using lossless mode during writing but I noticed that the resultant file size is ~6x the original image size even though the image is cropped to ~half the total original pixel count. Similarly, lossy export with 100% quality and half the number of pixels is ~2x the original image size. I can understand how saving subsequent lossy jpg files can compress information AND file size but it doesn't appear to be working this way. Maybe the compression/file writing scheme of Matlab and the original file differ? If so, maybe there is a file exchange that would allow for this scheme to be detected and remain consistent through repeated file exports?
-I am trying to speed up the batch export process using the 3 simple looped commands that are stated above. I think that if I can reduce the resultant lossless file size from my first point then I will speed up the biggest bottleneck which is writing the increased file size to desk. But maybe you have a more clever way to open and save a larger amount of images (~20k)? I do see other open-source software available for bulk image resizing but not necessarily bulk image cropping.

Answers (1)

Vineet Joshi
Vineet Joshi on 19 Jul 2021
Yes you are correct. When you read an image using imread, the function returns an array of size H * W * 3 and while writing using imwrite the compression scheme and parameters changes. You can control these by looking at some hyperparameters like 'Quality','Mode','CompressionRatio' etc.
Just to test, I ran the following code and got the output image size reduced by ~95% from 88Kb to 4Kb.
Moreover, since you have to perform operations on ~20K images and each iteration is independent of the previous, you can run them in parallel using the parfor and similar methods.
Hope this was of help.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!