Capturing frames from macbook isight with PIL - python

I'm trying to write an app using python and PIL that involves processing frames from my macbook camera. Is there an easy way to capture frames from the iSight for use with PIL without installing too many libraries?
I've seen a few questions on SO about doing this with OpenCV, which I don't want to do. Can I do this without using OpenCV as I've had trouble installing that?

Maybe you can try using the SimpleCV library. After installing it, the following code should work:
from SimpleCV import Camera
from SimpleCV import Image
webcam_camera = Camera()
webcam_image = webcam_camera.getImage()
webcam_image.save("frame.jpg")
Although OpenCV would be a much better option. If you follow the instructions on their website, it wouldn't be too hard to get it up and running.

Related

Is there a way to capture an image of the screen in python with ctypes?

I want to have python save an image file of the whole screen as a variable with ctypes so that I could access the screen in a program and do something with it (like put it on a pygame window). I can't use any other libraries unless they are included with python (no installing or pip). Does anyone know how to do this?
Edit: I'm using windows 10.
PIL.ImageGrab is from PILLOW (a python image library fork which you can install with pip). You can give a bounding box or capture the entire screen.
Update: OP now mentions he can't use external libraries.
Then you could virtually hit printscreen and read the clipboard. The code of PILLOW is open-source feel free to use it.
Remember that you can always call a command from within python:
>>> import os
>>> os.system("pip install pillow")
Or download the zip of the library and import it in your code.

Rotating a video with Python 3.4

I am using moviepy to try resize a video clip but every time I try I get this error. Can anyone explain how I can fix it? Thanks
My python code
Import everything needed to edit video clips
from moviepy.editor import *
# Load video clip
myclip = VideoFileClip("dog.mov")
myclip.resize( (460,720) ) # New resolution: (460,720)
myclip.write_videofile("resized_clip.mp4") #write new video file
The error
File "/usr/local/lib/python3.4/dist-packages/PIL/Image.py", line 699, in tostring
"Please call tobytes() instead.")
Exception: tostring() has been removed. Please call tobytes() instead.
Looks like you are using PIL, I would try using Pillow, a support fork that is maintained. MoviePY recommends you use Pillow in lieu of Pil in it's docs:
http://zulko.github.io/moviepy/install.html
For advanced image processing you will need one or several of these
packages. For instance using the method clip.resize requires that at
least one of Scipy, PIL, Pillow or OpenCV are installed.
The Python Imaging Library (PIL) or, better, its branch Pillow .

PIL decoder JPEG not available Raspberry

i'm trying to utilize PIL to open a jpeg image and assign it to a Tkinter's label.
However whenever i try to open the image i get the same problem as this guy
I tried all the suggestions he got and also the ones i found here but it doesn't seem to fix, by installing PIL or Pillow(i tried that too) during the setup i get :
*** TKINTER support not available
*** JPEG support not available
And whenever i run my code i get an IOError: decoder jpeg not available
I'm using python 2.7.
Can someone provide a good method to make PIL or Pillow work with jpeg support? I've been googling extensively for two days, but all the possible fixes that i found don't seem to work for me
Installing libjpeg-dev should do the trick, as proposed by the link you provided. But if it doesn't help (I ran into that as well) you can consider upgrading to Pillow 3, it looks like you are running Pillow 2. This also helped me getting rid of the errors, don't know why exactly..

Video Processing using Python and OpenCV

I am trying to capture a video and convert into frames using python and openCV. I followed the steps that need to done to import openCV2 libraries for python in windows 8 which is as follows:
Download Python, Numpy, OpenCV from their official sites.
Extract OpenCV (will be extracted to a folder opencv)
Copy ..\opencv\build\python\x86\2.7\cv2.pyd
Paste it in C:\Python27\Lib\site-packages
Open Python IDLE or terminal, and type
I have done the above steps to import opencv libraries into python.
But when I import CV2 libraries and try to capture a video, I was unable to generate frames using the function cv.CaptureFromFile() and accessing the frames using cv.QueryFrame() functions. I was unable to capture the frames. Please find the code below.
import sys
import os
import numpy as np
import PIL
from matplotlib import pyplot as plt
import cv2.cv as cv
winname = "myWindow"
win = cv.NamedWindow(winname, cv.CV_WINDOW_AUTOSIZE)
invideo = cv.CaptureFromFile("chayya1.avi")
totalNumberOfFrames = int(cv.GetCaptureProperty(invideo, cv.CV_CAP_PROP_FRAME_COUNT))
framesprocessing = totalNumberOfFrames
print(totalNumberOfFrames)
while (True):
im = cv.QueryFrame(invideo)
cv.ShowImage(winname, im)
if cv.WaitKey() == 27: # ASCII 27 is the ESC key
break
del invideo
cv.DestroyWindow(winname)
The output is getting processed without any errors but the frames are not generating and even the print statement for frame count is also zero.
I just want to what is procedure for installing OpenCV for python in windows and accessing the frames of a video using the obove mentioned functions.
Thanks in advance
Using opencv to read video files
As mentioned in the tutorial here, the recommended approach now is to use cv2 (not cv as you did) to read video files using, e.g.
import numpy as np
import cv2
cap = cv2.VideoCapture("chayya1.avi")
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Python & OpenCV installation on windows
For a simple solution to installing OpenCV's python bindings on windows you might want to try the Python(xy) distribution which bundles a bunch of python modules including opencv.
Python's opencv bindings are included with opencv, so if you didn't get any import errors then your opencv setup is likely to be correct.
Alternative bindings
If you don't like the native bindings, you could try using pip from the command line to install 3rd party bindings, though as berak pointed out this is not generally recommended;
pip install pyopencv
Pip itself can be installed as outlined in this answer

No highgui in python

I'm trying to write some programs using opencv and python. I installed opencv and the python libraries from the repositories. I'm using ubuntu 12.04. The folder /var/lib/python-support/python2.7 contains just 5 files :
cv2.so
cv.py
cv.pyc
simplegeneric-0.7.egg-info
simplegeneric.py
simplegeneric.pyc
From what reading I have done, I think there's supposed to be an opencv folder around here. I'm able to import cv library using
import cv
but
from opencv import cv
And I cannot load the highgui module. Any way to work around this? I would really really like to do something in opencv
You must have installed OpenCV >= 2.3.1. In OpenCV 2.3.1 and higher, Python bindings do not have highgui.
import cv2
import cv2.cv as cv
and you're good to go.
import cv2
img = cv2.imread("image name")
cv2.imshow("window name", img)
cv2.waitKey(0)
You can find more help on OpenCV docs and you could also look at some of the opwncv stuff that I have been doing here.
I hope this helps.
You will get the highgui file in OpenCV folder(installed folder) in include/opencv/highgui.h.

Categories