Skip to content

Instantly share code, notes, and snippets.

@pnck
Created April 8, 2018 06:26
Show Gist options
  • Save pnck/940f22e4e7d3ada7ee3e1b3af438ff38 to your computer and use it in GitHub Desktop.
Save pnck/940f22e4e7d3ada7ee3e1b3af438ff38 to your computer and use it in GitHub Desktop.
convert raw uyvy data captured by v4l2 devices to rgb bitmap file
#!/usr/bin/python2
import numpy as np
import cv2
import sys
import os
if len(sys.argv) < 2:
print 'nope ...\nusage:'+sys.argv[0]+' file_name\n\n'
for fname in sys.argv[1:]:
with open(fname,'rb') as f:
try:
print 'trying '+fname+' ...'
uyvyb = f.read()
uyvy = [ord(b) for b in uyvyb]
uyvyarr = zip(*[iter(zip(*[iter(uyvy)]*2))]*1280*2)
yuvnp = np.array(uyvyarr,'uint8')
img = cv2.cvtColor(yuvnp,cv2.COLOR_YUV2BGR_UYVY)
wfname = fname+'.bmp'
print 'convert '+fname+' successfully. writing to ' + wfname + '...'
cv2.imwrite(wfname,img)
os.unlink(fname)
print 'done.'
try:
from PIL import Image
with Image.open(wfname) as img:
cw = img.width / 2
ch = img.height / 2
regions = ((0,0,cw,ch),(cw,0,cw*2,ch),(0,ch,cw,ch*2),(cw,ch,cw*2,ch*2))
for idx,i in enumerate(map(img.crop,regions)):
s = wfname[:-4]+'_crop_'+str(idx)+'.bmp'
i.save(s)
print 'saved '+s
except:
print 'crop '+wfname+' failed..'
except:
print fname+' ...failed'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment