使用Python显示YUV420图像

由于网上没找到好用的显示YUV420semiplanar格式图像的工具,自己整理了一段显示YUV420图像的python代码,分享给大家。

###############################################################
# author: sl
# function: view yuv image file(only support YUV420SemiPlanar)
# date: 2016-8-30
###############################################################
from numpy import *
from PIL import Image

def readYuvFile(filename,width,height):
fp=open(filename,'rb')
uv_width=width//2
uv_height=height//2

Y=zeros((height,width),uint8,'C')
U=zeros((uv_height,uv_width),uint8,'C')
V=zeros((uv_height,uv_width),uint8,'C')

for m in range(height):
for n in range(width):
Y[m,n]=ord(fp.read(1))
for m in range(uv_height):
for n in range(uv_width):
V[m,n]=ord(fp.read(1))
U[m,n]=ord(fp.read(1))

fp.close()
return (Y,U,V)

def yuv2rgb(Y,U,V,width,height):
U=repeat(U,2,0)
U=repeat(U,2,1)
V=repeat(V,2,0)
V=repeat(V,2,1)
rf=zeros((height,width),float,'C')
gf=zeros((height,width),float,'C')
bf=zeros((height,width),float,'C')

rf=Y+1.14*(V-128.0)
gf=Y-0.395*(U-128.0)-0.581*(V-128.0)
bf=Y+2.032*(U-128.0)

for m in range(height):
for n in range(width):
if(rf[m,n]>255):
rf[m,n]=255;
if(gf[m,n]>255):
gf[m,n]=255;
if(bf[m,n]>255):
bf[m,n]=255;

r=rf.astype(uint8)
g=gf.astype(uint8)
b=bf.astype(uint8)
return (r,g,b)

if __name__=='__main__':
width=640
height=480
data=readYuvFile('/home/sl/xx.yuv',width,height)
Y=data[0]
im=Image.fromstring('L',(width,height),Y.tostring())
im.save('/home/sl/y.bmp')
im.show()

RGB=yuv2rgb(data[0],data[1],data[2],width,height)
im_r=Image.fromstring('L',(width,height),RGB[0].tostring())
im_g=Image.fromstring('L',(width,height),RGB[1].tostring())
im_b=Image.fromstring('L',(width,height),RGB[2].tostring())
im_rgb=Image.merge('RGB',(im_r,im_g,im_b))
im_rgb.save('/home/sl/rgb.bmp')
im_rgb.show()


更多文章,可查看个人博客:http://knowthendo.cn/ 点击打开链接


你可能感兴趣的:(图像处理,python)