我想将位图转换为 YUV422 (YUYV) 格式。我在谷歌上搜索了 YUYV 格式并尝试编写此代码。
path = "C:/Users/hogan/Desktop/red.bmp"
image = Image.open(path).convert("YCbCr") # YUV
image = np.asarray(image)
width, height, YUYV = image.shape[1], image.shape[0], 4
array = np.empty((height * width * 3), dtype="uint8")
Y, U, V = 0, 1, 2
count = 0
for i in range(len(image)):
for j in range(len(image[i])):
for k in range(len(image[i][j])):
if (count % 4 == 0):
array[count] = image[i][j][Y]
if (count % 4 == 1):
array[count] = image[i][j][U]
if (count % 4 == 2):
array[count] = image[i][j][Y]
if (count % 4 == 3):
array[count] = image[i][j][V]
count = count + 1
array.astype('uint8').tofile("C:/Users/hogan/Desktop/tmpdir/1.raw")
我读了这张图片,知道我的代码是错误的,但不知道如何改正。
例如:YUV 中的红色 (255,0,0) 是 (76,84,255),如果我有很多像素,我不知道应该删除哪个“U”和“V”。
如果使用我的代码转换 480*640 (W*H),它将是 960*480。