Search code examples
imagesizepng

I get the wrong image size after applying the size calculation formula


I am trying to calculate the size of image in K bytes based on the image width and height in pixel and the bit depth and check it with its size. I have applied 2 formulas:

  1. (width * height * bit depth ) / (8 * 1024 )
  2. (width * height * 8 ) / 1024

but every time I got a size result different from the on showed in the image properties window. For example:

first example

When applying the formulas I got a size different and way bigger than 467 Kb.

I am wondering if I am using the wrong formula. How can I get the same image size based on width, height and bit depth?


Solution

  • You gotta follow these steps:

    1. Multiply height and width (this will help you to know the total number of pixel)
    2. Multiply the result by bit depth (this will help you to get the total number of bits data)
    3. Divide the result by 8 (then you'll have the file size in bytes)
    4. Divide the result of bytes by 1024 (then you'll have the file size in kilobytes)

    So your example will be:

    1. (Height * width) = result1
    2. result1 * bit depth = result2
    3. result2 / 8 = result3
    4. result3 / 1024 = finalresult

    Finally we must have:

    (Height * width * bit depth) / (8 * 1024)
    

    If the bit depth is 8, you'll have:

    (Height * width * 8) / (8 * 1024) = (Height * width)/1024