Skip to content

Instantly share code, notes, and snippets.

@lp6m
Created September 10, 2018 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lp6m/209ab987e2bc25261d8b1c6ef1f3777d to your computer and use it in GitHub Desktop.
Save lp6m/209ab987e2bc25261d8b1c6ef1f3777d to your computer and use it in GitHub Desktop.
Convert Raw YUYV image from PCam 5C to png
#coding:utf-8
#usage: python yuv.py input_rawdata.dat output_filename.png
from PIL import Image
import sys
f = open(sys.argv[1],"rb")
outfile = sys.argv[2]
WIDTH = 1920
HEIGHT = 1080
im = Image.new("RGB", (WIDTH, HEIGHT))
x = y = 0
for index in range(WIDTH * HEIGHT / 2):
y1 = ord(f.read(1))
u = ord(f.read(1)) - 128
y2 = ord(f.read(1))
v = ord(f.read(1)) - 128
r1 = int(y1 + 1.140*v)
g1 = int(y1 - 0.395*u - 0.581*v)
b1 = int(y1 + 2.032*u)
r2 = int(y2 + 1.140*v)
g2 = int(y2 - 0.395*u - 0.581*v)
b2 = int(y2 + 2.032*u)
r1 = min(max(r1,0), 255)
g1 = min(max(g1,0), 255)
b1 = min(max(b1,0), 255)
r2 = min(max(r2,0), 255)
g2 = min(max(g2,0), 255)
b2 = min(max(b2,0), 255)
im.putpixel((x,y), (r1,g1,b1))
im.putpixel((x+1,y), (r2,g2,b2))
x = x + 2
if(x == WIDTH):
x = 0
y = y + 1
im.save(outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment