0
import cv2
import numpy as np

# load input image:
input = cv2.imread("image.png")  #image.png size has 1.48MB & bit depth: 8

# scaling:
scaled_img = cv2.resize(input,None,fx=0.5,fy=0.5,)
cv2.imshow("Scaling-Linear Interpolation", scaled_img)

#saving the img:
cv2.imwrite("image_resize.png", scaled_img)  # 1.66 MB & bit depth: 24

I want to know why bit depth changes from it's original image. And also know how to change according to user.

Harsh Goyal
  • 73
  • 1
  • 8
  • 1
    What is the color standard of `image.png`? Is it a Grayscale image? Is it an indexed image? Can you add the image to your post? – Rotem Dec 28 '19 at 14:12
  • Without inspecting the source image, it's impossible to give some detailed explanation. However, OpenCV by default uses a very low compression level (one step above no compression at all) when writing PNGs. It also doesn't support writing paletted images. – Dan Mašek Dec 28 '19 at 17:21
  • It is possible that your original image was a palette image https://stackoverflow.com/a/52307690/2836621 and OpenCV converts them to BGR images when you load them, but doesn't support writing palette images back to disk, so the disk file size increases even though the image dimensions in pixels are smaller. Use `exiftool YourInputImage.png` to tell what your input image is. – Mark Setchell Jan 02 '20 at 14:42

1 Answers1

0

opencv imread always reads as 3 channel input, so it adds 2 identical channels for a loaded grayscale image.

You can use the grayscale or unchanged flag to load as grayscale, or cv2.cvtColor to convert to grayscale.

see https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822 for flags

so currently you are loading a single channel 8 bit image file as a 3x8=24 bit image. After saving to png it is a 24 bit image file.

Micka
  • 19,585
  • 4
  • 56
  • 74
  • that i know but i want to know that "image_resize.png" 's resolution has decreased to the original one but size didn't and also bit depth has also changed i mentioned on code's comments. – Harsh Goyal Dec 28 '19 at 17:07
  • bit depth 24 bit is 3x8bit, because you loading an 1-channel image as 3-channel image and are saving as RGB image. If you load as "unchanged" or convert to grayscale after loading, you'll save as 8 bit. – Micka Dec 28 '19 at 18:43