将图像从 8 位转换为 10 位的问题

Problems Converting Images from 8-bit to 10-bit

我正在尝试将 8 位图像转换为 10 位图像。我认为这就像更改 bin 值一样简单。我试过枕头和 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)

虽然 cv2.imwrite 成功写入新文件,但它仍然被编码为 8 位图像,即使 bin 上升到 1023。

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

在 python 或 linux 中是否有其他方法可以将 8 位转换为 10 位?

这里有很多问题。

  1. 您无缘无故地将 OpenCV (cv2.imwrite) 与 PIL (Image.open) 混合使用。不要那样做,你会感到困惑,因为他们使用不同的 RGB/BGR 顺序和约定,

  2. 您正在尝试将 10 位数字存储在 8 位向量中,

  3. 您试图在 PIL 图像中保存 3 个 16 位 RGB 像素,will not work 因为 RGB 图像在 PIL 中必须是 8 位。


我建议:

import cv2
import numpy as np

# Load image
im = cv2.imread(IMAGE, cv2.IMREAD_COLOR)

res = im.astype(np.uint16) * 4
cv2.imwrite('result.png', res)

我找到了使用 pgmagick 包装器 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)