pythonopencvubuntupython-imaging-librarybit-depth

Problems Converting Images from 8-bit to 10-bit


I am trying to convert 8 bit images to 10 bit. I thought it would be as easy as changing the bin values. I've tried to pillow and cv-python:

from PIL import Image
from numpy import asarray
import cv2

path = 'path/to/image'
img = Image.open(path)
data = asarray(img)

newdata = (data/255)*1023 #2^10 is 1024
img2 = Image.fromarray(newdata) #this fails

cv2.imwrite('path/newimage.png, newdata)

While cv2.imwrite successfully writes the new file, it is still encoded as an 8bit image even though bin goes up to 1023.

$ file newimage.png
newimage.png: PNG Image data, 640 x 480, 8-bit/color RGB, non-interlaced

Is there another way in either python or linux that can convert 8-bit to 10-bit?


Solution

  • I found a solution using pgmagick wrapper for python

    import pgmagick as pgm
    
    imagePath = 'path/to/image.png'
    saveDir = '/path/to/save'
    
    img = pgm.Image(imagePath)
    img.depth(10) #sets to 10 bit
    
    save_path = os.path.join(saveDir,'.'.join([filename,'dpx']))
    img.write(save_path)