import numpy as np
from scipy import signal
from opencv import adaptors
class scipyFromOpenCV(object):
"""This decorator can be used to wrap a function that takes
and returns a numpy array into one that takes and retuns an
opencv CvMat.
"""
def __init__(self, f):
self.f = f
def __call__(self, image):
# Convert CvMat to ndarray
np_image = adaptors.Ipl2NumPy(image)
# Call the original function
np_image_filtered = self.f(np_image)
# Convert back to CvMat
return adaptors.NumPy2Ipl(np_image_filtered)
@scipyFromOpenCV
def slowGaussianBlur(matrix):
"""Manual gaussian blur - Very very very slow!"""
filterSize = 3
filt = gauss_kern(filterSize)
r = signal.convolve(matrix[:,:,0],filt,'same')
g = signal.convolve(matrix[:,:,1],filt,'same')
b = signal.convolve(matrix[:,:,2],filt,'same')
result = array([r,b,g]).astype(uint8).transpose((1,2,0))
return result
Today I had to automatically access my Gmail inbox from Python. I needed the ability to get an unread email count, the subjects of those unread emails and then download them. I found a Gmail.py library on sourceforge, but it actually opened the normal gmail webpage and site scraped the info. I wanted something much faster, luckily gmail can now be accessed with both pop and imap. After a tiny amount of research I decided imap was the better albiet slightly more difficult protocol. Enabling imap in gmail is straight forward, it was under labs. The address for gmail's imap server is: imap.gmail.com:993 Python has a library module called imaplib , we will make heavy use of that to access our emails. I'm going to assume that we have already defined two globals - username and password. To connect and login to the gmail server and select the inbox we can do: import imaplib imap_server = imaplib . IMAP4_SSL ( "imap.gmail.com" , 993 ) imap_server . login ( use...