Python – Why my grayscale image is larger than the color image

Why my grayscale image is larger than the color image… here is a solution to the problem.

Why my grayscale image is larger than the color image

Problematic image: http://cdn1.ouchpress.com/media/celebrities/539/kate-upton-401846.jpg

I’m trying to convert an image to grayscale using the following command

img = cv2.imread('kate.jpg',cv2. IMREAD_GRAYSCALE)

cv2.imwrite('kategray.jpg',img)

However, grayscale images have a size of 700kB, while color images have only 500kB.

What am I missing? Since the color information was removed, the alpha channel was also removed.

Shouldn’t images have smaller dimensions?

Solution

As they do in documentation As mentioned in , if you do not specify params, they will be set as defaults.

Here’s what they say:

For JPEG, it can be a quality ( CV_IMWRITE_JPEG_QUALITY ) from 0 to 100 (the higher is the better). Default value is 95.

So, here you go. JPEG is a compression format, so the file size depends on the compression ratio.

I assume that your original image has a compression ratio lower than the default 95 in opencv.

If you want to reduce the file size, try specifying a lower compression ratio as the third parameter by params:

(I assume a compression ratio of 85%)

cv2.imwrite('kategray.jpg', img, 85)

Related Problems and Solutions