user3144836
user3144836

Reputation: 4148

JPEG image memory byte size from OpenCV imread doesn't seem right

Using OpenCV, Whenever I save an image (from a camera stream) as JPG to the disk. The file size in bytes on the disk differs depending on the JPEG quality as expected.

However, whenever I read the images regardless of the file size on the disk, the memory size remains constant. Is this normal?

Also, there seems to be a massive difference in the disk and memory size of the image. I was expecting a much smaller memory size, somewhat relative to the disk size.

For e.g.

import sys
import cv2

cv2.imwrite('temp.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 10])
# disk size of temp.jpg is 24kb

image = cv2.imread('temp.jpg')
# memory size is 2.7 mb

cv2.imwrite('temp.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
# disk size of temp.jpg is 150kb

image = cv2.imread('temp.jpg')
# memory size is still constant at 2.7 mb

This is how I compute the memory size:

print("Image byte size :", round(sys.getsizeof(image) / (1024 * 1024), 2), "mb")

Upvotes: 1

Views: 3735

Answers (1)

ZdaR
ZdaR

Reputation: 22954

Yes, it is completely normal. All the image formats are actually different ways of compressing a m x n RGB matrix data. JPG is relatively better than PNG in terms of space. Also note that this relative behaviour comes at a cost, which is: JPG is lossy compression technique but PNG is a lossless compression technique.

When we read a JPG or PNG image from disk, it is first uncompressed and then the matrix data is populated. The in memory size would always be: m x n x 3 bytes, but the disk size would be different depending upon the compression format used, compression level used, etc. It can also vary for different image gradients. An image with a single color would take much less space in JPG format than an image with lot of colors (gradient images). But the memory size of both the images would be same: m x n x 3 bytes.

Upvotes: 5

Related Questions