0

我正在尝试将 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成功写入新文件,但即使 bin 达到 1023,它仍被编码为 8 位图像。

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

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

4

2 回答 2

3

这里有很多事情出错了。

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

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

  3. 您试图在 PIL 图像中保存 3 个 16 位 RGB 像素,这将不起作用,因为 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)
于 2020-10-26T17:04:22.557 回答
0

我找到了一个使用pgmagickwrapper 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)
于 2020-11-06T15:29:49.507 回答