如何从16位LE数据中提取这些位?

发布于 2025-01-28 11:57:16 字数 638 浏览 4 评论 0原文

我有 16位数据在以下le位格式中:

b4 | B5 | C1 | C2 | C3 | D1 | D2 | D3

A1 | A2 | A3 | A4 | A5 | B1 | B2 | B3

每个字母代表我要提取的一个数据类别,并从中制作单独的图像。

使用此Python代码,我设法从A层创建了一个图像,但是我没有成功提取B,C和D。

# using numpy and PIL
data = np.fromfile(i, dtype=np.dtype('<u2')).reshape(size, size)
A = ((data & 31) - 1).astype('uint8')
image_A = Image.fromarray(A)

有人知道这将如何工作吗?

示例数据(512x512)输出a

I've got 16 bit data in the following LE bit format:

B4 | B5 | C1 | C2 | C3 | D1 | D2 | D3

A1 | A2 | A3 | A4 | A5 | B1 | B2 | B3

Each letter represents one data category that I want to extract and make a seperate image from.

Using this python code, I managed to create an image from the A layer, but I did not succeed in extracting B, C and D.

# using numpy and PIL
data = np.fromfile(i, dtype=np.dtype('<u2')).reshape(size, size)
A = ((data & 31) - 1).astype('uint8')
image_A = Image.fromarray(A)

Does anyone know how that would work?

Sample data (512x512), Output A

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⒈起吃苦の倖褔 2025-02-04 11:57:16

使用bitmasks的字典和一些twiddling来计算必要的变化:

import numpy as np
from PIL import Image

def extract_mask(arr, mask):
    # bit twiddling magic (count trailing zeros)
    shift = int(np.log2(mask & -mask))
    return (arr & mask) >> shift

masks = {
    "A": 0b000_000_00000_11111,
    "B": 0b000_000_11111_00000,
    "C": 0b000_111_00000_00000,
    "D": 0b111_000_00000_00000,
}

filename = "512x512.buffer"
size = 512
data = np.fromfile(filename, dtype="<u2").reshape(size, size)
images = {
    k: Image.fromarray(extract_mask(data, mask).astype(np.uint8))
    for k, mask in masks.items()
}

Using a dictionary of bitmasks and some bit twiddling to compute the requisite shifts:

import numpy as np
from PIL import Image

def extract_mask(arr, mask):
    # bit twiddling magic (count trailing zeros)
    shift = int(np.log2(mask & -mask))
    return (arr & mask) >> shift

masks = {
    "A": 0b000_000_00000_11111,
    "B": 0b000_000_11111_00000,
    "C": 0b000_111_00000_00000,
    "D": 0b111_000_00000_00000,
}

filename = "512x512.buffer"
size = 512
data = np.fromfile(filename, dtype="<u2").reshape(size, size)
images = {
    k: Image.fromarray(extract_mask(data, mask).astype(np.uint8))
    for k, mask in masks.items()
}
筱果果 2025-02-04 11:57:16

这似乎是一种简单的方法:

import numpy as np
from PIL import Image

# Load image as 16-bit LE and reshape
size = 512
data = np.fromfile('512x512.buffer', dtype='<u2').reshape(size, size)

A = Image.fromarray(((data      ) & 31).astype(np.uint8))
B = Image.fromarray(((data >>  5) & 31).astype(np.uint8))
C = Image.fromarray(((data >> 10) &  7).astype(np.uint8))
D = Image.fromarray(((data >> 13) &  7).astype(np.uint8))

This seems an easy way to do it:

import numpy as np
from PIL import Image

# Load image as 16-bit LE and reshape
size = 512
data = np.fromfile('512x512.buffer', dtype='<u2').reshape(size, size)

A = Image.fromarray(((data      ) & 31).astype(np.uint8))
B = Image.fromarray(((data >>  5) & 31).astype(np.uint8))
C = Image.fromarray(((data >> 10) &  7).astype(np.uint8))
D = Image.fromarray(((data >> 13) &  7).astype(np.uint8))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文