T

#### ttest

I'm working on an image processing project using the Python Imaging

Library along with numpy. Right now, I'm trying to build a speedy

script for converting whole images between the RGB and the HSV (a.k.a.

HSB) color spaces. Unfortunately, the code I've made so far runs

dreadfully slow with even moderate-sized images.

I'm under the impression that the crux of the problem is the fact that

PIL's point method operates on only one band at a time, and to do a

proper color space conversion, you need information about all three

bands in a particular problem. This has forced me to do an awkward

work-around where the image data is loaded into a numpy array

(1600x1200x3), and a dinky for-loop run runs through the array picking

up RGB values, converting to HSV, and dumping the results back into

another array.

How can I make this more efficient?

--------

from PIL import Image

from PIL import TiffImagePlugin

from scipy.misc import pilutil

import numpy

import colorsys

def myrgbtohsv(r,g,b):

'''A wrapper for the colorsys function rgb_to_hsv

that takes r,g,b values between 0 and 255

(standard pixel value range in most of my single-band imaging

operations)

and returns the corresponding 255-scaled h,s,v values'''

tr = r/255.0

tg = g/255.0

tb = b/255.0

ur,ug,ub = colorsys.rgb_to_hsv(tr,tg,tb)

pr = int(round(ur,0))*255

pg = int(round(ug,0))*255

pb = int(round(ub,0))*255

return pr,pg,pb

def rgbarrtohsvarr(pilarray):

'''Takes an 3d numpy ndarray loaded with a RGB PIL image and

converts

a copy of the contents to 255-scaled HSV array.

Returns the converted copy of the array.

'''

arrt = pilarray.copy()

r,c,d = arrt.shape

hsvarray = numpy.zeros((r,c,d))

print out

for i in range(0,r):

for j in range(0,c):

localrgb = (arrt[i,j,0],arrt[i,j,1],arrt[i,j,2])

(lh,ls,lv) = rgbtohsv(localrgb)

hsvarray[i,j,0],hsvarray[i,j,1],hsvarray[i,j,2]= lh,ls,lv

return hsvarray

im = Image.open(r'C:\test.tif')

arr = pilutil.fromimage(im)

out = rgbarrtohsvarr(arr)

--------