#pip install opencv-python# from PIL import Image
from PIL import Image
import cv2
import numpy as npfile_path='/home/pozhu/Desktop/dog.jpeg'
img= Image.open(file_path)# img_nv12 = img.convert('NV12')# img_nv12.save("output.nv12")def bgr2nv12(image): image = image.astype(np.uint8) height, width = image.shape[0], image.shape[1] yuv420p = cv2.cvtColor(image, cv2.COLOR_BGR2YUV_I420).reshape((height * width * 3 // 2, )) y = yuv420p[:height * width] uv_planar = yuv420p[height * width:].reshape((2, height * width // 4)) uv_packed = uv_planar.transpose((1, 0)).reshape((height * width // 2, )) nv12 = np.zeros_like(yuv420p) nv12[:height * width] = y nv12[height * width:] = uv_packed return nv12image_cv = cv2.imread(file_path)
nv12_image= bgr2nv12(image_cv)
nv12_image_path="output_"+str(image_cv.shape[1])+'_'+str(image_cv.shape[0]) + ".nv12"
nv12_image.tofile(nv12_image_path)
OpenGL 进行 RGBA 转 NV12
https://blog.csdn.net/weixin_48711201/article/details/132360752