dan-masek

Untitled

May 10th, 2021
557
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from cv2_45 import cv2
  2. import numpy as np
  3.  
  4. img_cv = cv2.imread('example_image.tiff', cv2.IMREAD_ANYDEPTH)
  5. img_cv = img_cv.view(np.int32)
  6. print(img_cv.max()) # 40950
  7.  
  8. # Since the max_value is less than 2^16, let's cast it to 16bit
  9. img_cv_16bit = img_cv.astype(np.uint16)
  10.  
  11. cv2.imwrite('output_cv_16bit.png', img_cv_16bit)
  12.  
  13. # Actually, it looks like those max values are just spurious extra bright spots.
  14. # We can probably clip it to 8bit range... if we scale it down by 16
  15.  
  16. img_cv_8bit = np.clip(img_cv_16bit // 16, 0, 255).astype(np.uint8)
  17.  
  18. cv2.imwrite('output_cv_8bit.png', img_cv_8bit)
  19.  
  20.  
  21. # =========
  22. # And just for demonstration, PIL reads it as int32 with the same max
  23.  
  24. from PIL import Image
  25.  
  26. img_pil = Image.open('example_image.tiff')
  27. img_pil_cv = np.array(img_pil)
  28. print(img_pil_cv.dtype) # int32
  29. print(img_pil_cv.max()) # 40950
  30.  
  31.  
  32.  
RAW Paste Data