Images and Vision in Python: Slides from talk at Python Edinburgh Mini-Conf 2011

28 May
2011
, , ,

Last weekend the Python Edinburgh users group hosted a mini-conference. Saturday morning was kicked off with a series of talks followed by sessions introducing and then focusing on contributing to django prior to sprints which really got going on the Sunday.

The slides for my talk on, "Images and Vision in Python" are now available in pdf format here.

The slide deck I used is relatively lightweight with my focus being on demonstrating using the different packages available. The code I went through is below.

from PIL import Image

#Open an image and show it
pil1 = Image.open('filename')
pil1.show()

#Get its size
pil1.size
#Resize
pil1s = pil1.resize((100,100))
#or - thumbnail
pil1.thumbnail((100,100), Image.ANTIALIAS)

#New image
bg = Image.new('RGB', (500,500), '#ffffff')

#Two ways of accessing the pixels
#getpixel/putpixel and load
#load is faster
pix = bg.load()

for a in range(100, 200):
	for b in range(100,110):
		pix[a,b] = (0,0,255)
bg.show()

#Drawing shapes is slightly more involved
from PIL import ImageDraw
draw = ImageDraw.Draw(bg)
draw.ellipse((300,300,320,320), fill='#ff0000')
bg.show()

from PIL import ImageFont
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 72)
draw.text((10,10), "Hello", font=font, fill='#00ff00')
bg.show()


#Demo's for vision
from scipy import ndimage
import mahotas

#Create a sample image
v1 = np.zeros((10,10), bool)
v1[1:4,1:4] = True
v1[4:7,2:6] = True
imshow(v1, interpolation="Nearest")
imshow(mahotas.dilate(v1), interpolation="Nearest")
imshow(mahotas.erode(v1), interpolation="Nearest")
imshow(mahotas.thin(v1), interpolation="Nearest")

#Opening, closing and top-hat as combinations of dilate and erode

#Labeling
#Latest version of mahotas has a label func
v1[8:,8:] = True
imshow(v1)
labeled, nr_obj = ndimage.label(v1)
nr_obj
imshow(labeled, interpolation="Nearest")
pylab.jet()

#Thresholding
#Convert a grayscale image to a binary image
v2 = mahotas.imread("/home/jonathan/openplaques/blueness_images/1.jpg")
T = mahotas.otsu(v2)
imshow(v2)
imshow(v2 > T)

#Distance Transforms
dist = mahotas.distance(v2 > T)
imshow(dist)

Comments