/* work inprogress ;p */

While I may be a fan of embedded systems, I have to admit that python has become my new love. Thought taking into account that you can do sweet Matlab style dsp with it, you can’t really blame me. Assuming you have raw data from an image, you can do some awesome stuff with it using python. You will of course need python 2.6 as well as the numPy module installed. The techniques I’m going to discuss are already out there if you look for Matlab references but I thought I’d bring to light that you can do the same things using python as well. You will need to go to http://www.pythonware.com/products/pil/ to get the image module. After installing, I will suggest that you compile your entire python directory. To do that, open the python console and type:

import compileall
compileall.compile_dir(‘C:\python26′,force = True,maxlevels=20)

Now,lets start off simple by doing some masking. However, you should first create a 100×100 maroonish jpg in mspaint

Open your python console and type the following

from numpy import *
import Image

#import the jpg
im = Image.open(“test.jpg”)

#split the image into numpy arrays so we can do all kinds of matrix voodoo on it
[r,g,b] = [array(im.split()[i] for i in range(3)]
image = array([r,g,b])

# create a mask and define the region we will want to keep when we mask our image
mask = zeros((13,13),dtype=int) # make a 100×100 integer array of zeros
mask[0:25,0:25] = 0xFF #define the positive mask region noting that RGB has a max value of FF,FF,FF

The upper corner of your image should now be maroon while the rest of your image is white.
test = array[0:3] & mask

This entry was posted on Friday, November 13th, 2009 at 6:57 am and is filed under comp-e. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply