18 April, 2011

python code for taking images with webcam in Linux

Hey friends lets checkout a small, simple and cute python program for taking photos with with your webcam without the help of any third party application programs. Most probably the Ubuntu like linux distributions comes without a pre-installed application for imaging with webcam. I suggest this small piece of code because most of the linux distributions includes python  as pre-installed.

I have simplified the code to the extend that any newbie to python can understand it. Also you should install the following python library for this program to work. 

 # python-opencv
        You can install this library using synaptic package manager or apt. The package name is 'python-opencv'


Code


                                                                                                                                                                                       



   import os
   import datetime
   import opencv.adaptors
   from opencv import highgui 
   import Image

   def get_image(camera):
im = highgui.cvQueryFrame(camera)
      # Add the line below if you need it (Ubuntu 8.04+)
     im = opencv.cvGetMat(im)
     #convert Ipl image to PIL image
dir(im)
print "image taken"
     return opencv.adaptors.Ipl2PIL(im) 

   #_______________find usernae___________
   a=os.popen('whoami')
   l=a.read() 
   s=l.split('\n')
   uname=s[0]

   #______________find date n time  _________
   now = datetime.datetime.now()
   now = str(now)
  
   im=None
   camera= highgui.cvCreateCameraCapture(0)
   im = get_image(camera)
   os.chdir('/home/%s/Pictures' %(uname) )
   fname='image_'+now+'.png'
   im.save(fname, "PNG")
   ####  code ends here
                                                                                                                                                                                     


You can run this program using the terminal as follows

                                                           python camera.py                                                                                         


The images will be automatically stored inside the Pictures folder inside the home directory.


till the next post...

1 comments:

whocares on Sunday, May 27, 2012 said...

fantastic thank you. Have not tried it yet but hoping to use it soon :)

Post a Comment

speak out... itz your time !!!

 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger