Determine Final File Size

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
driventoofar
Posts: 6
Joined: 2019-11-20T11:30:17-07:00
Authentication code: 1152

Determine Final File Size

Post by driventoofar »

I think I already know the answer but I wanted to check just in case.

In my application a user can sometimes generate large size files based on how many images someone adds to it. This can keep the user from sending the file to an external API as they have size limits. What I would like to do is see if I can determine what the size of the file will be based on the images I'm going to create it with, before actually creating. Based on this I would create multiple files instead if it exceeds a certain limit. I don't think this is possible but wanted to see. At this point my only other option is to check the number of images and split based on that but that is more of an arbitrary value.

snibgo
Posts: 12922
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Determine Final File Size

Post by snibgo »

If the generated files are uncompressed, the size is easily calculated from the number of pixels.

For JPG, you can specify a maximum size, and IM will find the quality that delivers that size.

Otherwise, the easiest method is to create the file and find its size. I suppose your application might estimate the file size, but I doubt if that would be accurate.
snibgo's IM pages: im.snibgo.com

driventoofar
Posts: 6
Joined: 2019-11-20T11:30:17-07:00
Authentication code: 1152

Re: Determine Final File Size

Post by driventoofar »

Thank you for the response. My application deals specifically with pdf and tiff; can this be accomplished the same way as with jpg? And an estimation is good enough for me as long as it's close enough.

snibgo
Posts: 12922
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Determine Final File Size

Post by snibgo »

TIFF has a choice of no compression, or JPEG compression, or Zip compression etc.

PDF is simply a wrapper around a raster image format, which could be compressed with JPEG or Zip or whatever.

For good quality JPEG compression of ordinary colour photos, I need about 0.3 to 0.5 bytes per pixel. So a 1000x1000 pixel image will have a jpeg around 300-500 kB.
snibgo's IM pages: im.snibgo.com

Post Reply