Why image filesize changes after writing to Blob?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
vladimir_ff
Posts: 18
Joined: 2011-11-18T05:12:05-07:00
Authentication code: 8675308

Why image filesize changes after writing to Blob?

Post by vladimir_ff »

I have an image https://dl.dropbox.com/u/79309031/image.jpg
The size of file is 1650 bytes.
My code:

Code: Select all

Image newimage( "image.jpg" );
cout << newimage.fileSize() << endl;
Blob blob1;
newimage.write(&blob1);
cout << newimage.fileSize() << endl;
And the output is:
1650
1653
What happened?
Why filesize has changed after writing to blob?
IM 6.7.6.9
Ubuntu 11.04
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Why image filesize changes after writing to Blob?

Post by anthony »

JPG like most image file formats have a dynamic compression algorthim.

That means the file size depends on so many factors it is next to imposible to guarentte its size is predictable.

Image changes, meta-data changes, version of IM, version of coder, version of library, quality, colorspace, etc etc etc all will likely change a images file size.

A 3 byte difference on a read-write loop... I'd say you were lucky it wasn't bigger (or smaller)!


JPEG especially is a lossy compression... every time you write it the image gets modified. as such it ALWAYS changes on every read-write cycle as every time you do read and write JPG the image degrades a bit more.

JPEG should never be used for you main image storage, especially your source image, and for intermediate image processing work. It should only be used for final image, due to its small size.

See IM Examples, JPEG, Compression
http://www.imagemagick.org/Usage/formats/#jpg

Actually I seem to need to mention this on just about EVERY Im Examples page!

In summery, don't use JPEG unless you really need to.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply