我有以下 LE 位格式的16 位資料:
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
uj5u.com熱心網友回復:
使用位掩碼字典和一些位旋轉來計算必要的移位:
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()
}
uj5u.com熱心網友回復:
這似乎是一種簡單的方法:
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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/473674.html
