user1940257
user1940257

Reputation:

Calculating jpeg size from dimensions and bit depth

I have jpeg file with following details ExampleJPEG File

The file size is 1342 bytes as shown above and also confirmed by Hex Editor.

I was thinking i could also calculate the file size by using its dimensions and bit depth. i.e Total Bytes = ( Width X Height X bitDepth ) / 8

(28 X 28 X 24) / 8 = 2352 bytes

Why is this difference? The calculation formula says 2352 bytes whereas actual size is 1342 bytes. What am i missing?

Upvotes: 1

Views: 1890

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207485

When expanded out (decoded) and held as bytes in memory (RAM), the image will indeed occupy 2,352 bytes. But JPEGs are compressed to save space on disk, camera SD cards and when being Emailed and transmitted, so they are smaller and take less space/bandwidth.


If you want to see the pixel intensities in their expanded out RGB format, you can use a tool such as ImageMagick in the Terminal/command-line. Let's make a red image 28x28 - I'll make it in PNG format for now:

magick -size 28x28 xc:red PNG24:image.png

enter image description here

Now, if you want to view the pixels, you can either convert the image to a text dump with ImageMagick like this:

magick image.png -depth 8 -colorspace rgb txt:

Sample Output

# ImageMagick pixel enumeration: 28,28,65535,rgb
0,0: (255,0,0)  #FF0000  rgb(255,0,0)
1,0: (255,0,0)  #FF0000  rgb(255,0,0)
2,0: (255,0,0)  #FF0000  rgb(255,0,0)
3,0: (255,0,0)  #FF0000  rgb(255,0,0)
...
...
24,27: (255,0,0)  #FF0000  rgb(255,0,0)
25,27: (255,0,0)  #FF0000  rgb(255,0,0)
26,27: (255,0,0)  #FF0000  rgb(255,0,0)
27,27: (255,0,0)  #FF0000  rgb(255,0,0)

Or, if you want to view them in a more raw form of hex:

convert image.png -depth 8 rgb:image.raw

and view image.raw in a hex editor, or use Linux built-in tools:

convert image.png -depth 8 rgb: | xxd -g 3 -c12 | more
00000000: ff0000 ff0000 ff0000 ff0000  ............
0000000c: ff0000 ff0000 ff0000 ff0000  ............
00000018: ff0000 ff0000 ff0000 ff0000  ............

Note that if you check the size of the raw, uncompressed (decoded) image, you will see it happily matches your calculation of 2,352 bytes:

ls -l image.raw
-rw-r--r--  1 mark  staff  2352 27 Aug 09:56 image.raw

Note that I used PNG rather than JPEG above because JPEG is lossy and you don't get back what you save, you get something that looks similar but takes less space - like I said above.

Here is an example. I'll draw a red and a green line on a black background and save as a PNG and count the colours:

magick -size 100x100 xc:black +antialias -fill red -draw "line 5,5 95" -fill lime -draw "line 95,5 5,95" image.png

enter image description here

Now count the colours:

magick -format %k image.png info:
3

Now do exactly he same thing as a lossy JPEG:

magick -size 100x100 xc:black +antialias -fill red -draw "line 5,5 95,95" image.jpg

And count the colours in the JPEG and there are 148 now:

magick -format %k image.jpg info:
148

Keywords: Image Processing, ImageMagick, JPEG, lossy, PNG lossless, prime.

Upvotes: 2

Related Questions