1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| #coding:latin1
### Infos ###
_version_ = "0.0.1"
_author_ = "Franck Awounang Nekdem"
_name_ = "VCapture"
_description_ = "Visualiseur à base de VideoCapture pour Tkinter"
### ----- ###
import PIL.ImageTk as ImageTk
import PIL.ImageEnhance as ImageEnhance
import PIL.ImageGrab as ImageGrab
import time
import tkinter as Tkinter
import threading
### Constants Definition ###
_QUANTUM_GET = 0.2
_QUANTUM_SET = 0.2
_DEFAULT_SIZE = (1280, 800)
### END Definition ###
class Device:
"""Create instances of this class which will then represent video devices.
For the lifetime of the instance, the device is blocked, so it can not be
used by other applications (which is quite normal Windows behavior).
If you want to access the device from another program, you have to delete
the instance first (e.g. del cam).
"""
def __init__(self, devnum=0,size=_DEFAULT_SIZE,color=False):
"""devnum: VideoCapture enumerates the available video capture devices
on your system. If you have more than one device, specify
the desired one here. The device number starts from 0.
size: Size of the visualisator of the capture device.
"""
### End Redirections ###
### Fields definition ###
self.color = color
self.alive = True
self.size = size
self.master = Tkinter.Tk()
self.master.resizable(False,False)
self.can = Tkinter.Canvas(self.master,width=size[0],height=size[1],bg="black")
self.can.pack()
self.img = ImageTk.Image.new("RGB",size,0)
self.photo = ImageTk.PhotoImage(self.img, self.size, master = self.can)
self.id = self.can.create_image(size[0]/2+1,size[1]/2+1,image=self.photo)
self.nb_image = 0
self.current_image = 0
self.devnum = devnum
self.master.wm_protocol("WM_DELETE_WINDOW",self.term)
self.master.bind("<Escape>",self.term)
self.mainloop = self.can.mainloop
def _loop_(self):
try:
while self.alive:
if 1:#self.nb_image>self.current_image:
self.current_image += 1
self.photo = ImageTk.PhotoImage(self.img, self.size, master=self.master)
self.can.itemconfigure(self.id,image=self.photo)
self.can.delete(self.id)
n_id = self.can.create_image(self.size[0]/2+1, self.size[1]/2+1, image=self.photo)
self.id = n_id
self.can.lift(self.id)
time.sleep(_QUANTUM_GET)
except:
print('erreur')
self._loop_()
def _setImages_(self):
try:
while self.alive:
self.nb_image += 1
img = self.getImage()
if (img.size != self.size):
self.img = img.resize(self.size)
else:
self.img = img
time.sleep(_QUANTUM_SET)
except:
print('erreur')
self._setImage_()
def getImage(self):
"""Returns a PIL Image instance."""
####################################
# important #
####################################
a=ImageGrab.grab()
buffer=a.tobytes()
width, height = self.size
if buffer:
if self.color:
return ImageTk.Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
else:
return ImageEnhance.Color(ImageTk.Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)).enhance(self.color)
def inited(self):
"""Tels if the device capture have been found and can be used."""
return hasattr(self,"master")
def saveSnapshot(self, filename, **keywords):
"""Saves a snapshot to the harddisk.
The filetype depends on the filename extension. Everything that PIL
can handle can be specified (foo.jpg, foo.gif, foo.bmp, ...).
filename: String containing the name of the resulting file.
Additional keyword arguments can be give which are just passed to the
save() method of the Image class. For example you can specify the
compression level of a JPEG image by quality=75 (which is the default
value anyway).
"""
self.getImage().save(filename, **keywords)
def start(self):
"""Start capturing"""
self._Trd1 = threading.Thread(None,self._setImages_,)
self._Trd2 = threading.Thread(None,self._loop_)
self._Trd1.start()
self._Trd2.start()
def term(self,event=None):
"""Stop the cam and destroy the windows"""
self.alive = False
self.cam = None
if self.inited():
try:
self.master.destroy()
self._Trd1._Thread__stop()
self._Trd2._Thread__stop()
except :
pass
if __name__ == "__main__":
cam = Device(0,color=True)
cam.start()
cam.mainloop()
cam.master.quit()
del cam |
Partager