【发布时间】:2017-05-27 04:52:33
【问题描述】:
我正在尝试将图像转换为以下 DDS 格式:
| Resource Format | dwFlags | dwRGBBitCount | dwRBitMask | dwGBitMask | dwBBitMask | dwABitMask |
+-----------------+----------+---------------+------------+------------+------------+------------+
| D3DFMT_A4R4G4B4 | DDS_RGBA | 16 | 0xf00 | 0xf0 | 0xf | 0xf000 |
D3DFMT_A4R4G4B4 16 位 ARGB 像素格式,每个通道 4 位。
我有这个 python 代码(使用 Wand lib):
# source is jpeg converted to RGBA format (wand only supports RGBA not ARGB)
blob = img.make_blob(format="RGBA")
for x in range(0, img.width * img.height * 4, 4):
r = blob[x]
g = blob[x + 1]
b = blob[x + 2]
a = blob[x + 3]
# a=255 r=91 g=144 b=72
pixel = (a << 12 | r << 8 | g << 4 | b) & 0xffff
我得到的第一个像素是64328,但我期待的是62868。
问题:
- 我的 RGBA 到 ARGB 转换有误吗?
- 为什么我没有得到想要的结果?
【问题讨论】:
-
r,g,b 值以字节(8 位)为单位,您需要在重新组装像素之前将它们缩小到 4 位(0-16)。
-
ps 如果您这样做是为了处理图像,而不仅仅是为了家庭作业,使用 opencv (import cv2) 会更快更容易
-
@MartinBeckett 谢谢,终于弄明白了。虽然看起来有点差别,但输出和预期输出的差别非常细微。
标签: python python-3.x image-processing bitmask argb