List Combinations

2008-03-12 Thread Gerdus van Zyl
I have a list that looks like this:
[['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]

how can I get all the combinations thereof that looks like as follows:
3,9,5,4,2
3,1,5,4,2
3,9,5,4,5
3,1,5,4,5
etc.

Thank You,
Gerdus
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New UI Toolkit

2007-08-26 Thread Gerdus van Zyl

> But I'm afraid with the Swing-like interface, i.e : did you use the same
>   widget positionning ?

Not sure what you mean, but each parent widget is responsible for
rendering and positioning the children. Can use layout managers, two
currently absolute and simple flow. ( In the screenshot, the gradients
are in a flow panel)

-- 
http://mail.python.org/mailman/listinfo/python-list


New UI Toolkit

2007-08-26 Thread Gerdus van Zyl
Hi

I am halfway to a first release of a new GUI library for python. It
will be cross platform and follows the Swing philosophy of user
experience and interface fidelity above "but it doesn't look like
windows!" (aside: neither does office 2007 or windowsmediaplayer).

The library is built on top of CairoGraphics (cairographics.org) and
currently has a rather stable backend for Win32 and experimental
backends for GTK,Pyglet,pygame.

I am also busy with an WYSIWYG designer ala visualstudio but have yet
to decide on an serializable format leaning towards xul-alike without
XBL.

You can see a screenshot here: http://infireal.com/external/gradripper.png
and a executable of a utility built using here: http://www.infireal.com/alpha
(generates cairo/svg gradients from images)

Please reply and let your thoughts be known. Is there a need for a new
GUI library for python?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pure python gaussian blur

2007-08-25 Thread Gerdus van Zyl
Ok, now answering my own question :-)
Gaussian blurs can be optimized by seperating the equation and
applying in two passes.
I also implemented a version using State Vector Machines from www-
personal.engin.umd.umich.edu/~jwvm/ece581/21_GBlur.pdf but it does not
produce correct results. Most probably my fault, but it is 4x faster.
(code available on request)

Then after all this i learnt that using multiple box blurs approximate
a gaussian blur within 3%. This is the solution i use now and is fast
enough for my needs and in pure python.

Code for gaussian blur and box blur below.

~Gerdus van Zyl

Seperated Gaussian blur:

def blurB(self):

pixels = self.array
temp1 = copy.copy(pixels)
#temp2 = copy.copy(pixels)
stride = self.width * 4

clr = array.array('B',[255,255,255,255])

w = self.width
h = self.height

def pascal(n):
"""Prints n first lines of Pascal`s triangle

author: DR#m 
last rev 20.03.05"""
#n += 1

l=[1]
p=[]
for i in xrange(n):
l2=[1]
for j in xrange(len(l)-1):
l2.append(l[j]+l[j+1])
l2.append(1)
#print l
l=l2
return l


N = 5
g = pascal(N)
print '>',g

gauss_fact = g
#gauss_fact = [1,2,1]
#gauss_fact = [1,1]
gauss_width  = len(gauss_fact)
s = 0
for n in gauss_fact:
s += n
gauss_sum = s



print "Pass01"
for i in range(1,w-1):
#print i," of ",w
for j in range(1,h-1):
sumr=0
sumg=0
sumb=0
for k in range(0,gauss_width):
x = i-((gauss_width-1)>>1)+k
y = j
offset = y * stride + x * 4
pixel = pixels[offset:offset
+4]

r = pixel[0]
g = pixel[1]
b = pixel[2]

sumr+=r*gauss_fact[k]
sumg+=g*gauss_fact[k]
sumb+=b*gauss_fact[k]

clr[0] = sumr/gauss_sum
clr[1] = sumg/gauss_sum
clr[2] = sumb/gauss_sum

x = i
y = j
offset = y * stride + x * 4
temp1[offset:offset+4] =
clr

print "Pass02"
for i in range(1,w-1):
#print i," of ",w
for j in range(1,h-1):
sumr=0
sumg=0
sumb=0
for k in range(0,gauss_width):
x = i-((gauss_width-1)>>1)+k
y = j

offset = y * stride + x * 4
pixel = temp1[offset:offset+4]

r = pixel[0]
g = pixel[1]
b = pixel[2]

sumr+=r*gauss_fact[k]
sumg+=g*gauss_fact[k]
sumb+=b*gauss_fact[k]

clr[0] = sumr/gauss_sum
clr[1] = sumg/gauss_sum
clr[2] = sumb/gauss_sum

self.setPixel(i,j,clr) #to temp1


Box Blur:
def _boxBlur(self):

N = 5
xsum = 0
xsumar = []
pixels = self.array
opixels = copy.copy(pixels)

stride = self.width * 4

clr = array.array('B',[255,255,255,255])

w = self.width
h = self.height

pixel = 0

for rgb in [0,1,2]:
for x in range(0,w):
#print i," of ",w
for y in range(0,h):

hN = N/2
offset = (y-hN) * stride + (x-hN) * 4
if offset < 0: offset = 0
pixel = pixels[offset+rgb]

xsumar.append(pixel)

if len(xsumar) > N:
xsumar = xsumar[-N:]

xsum = 0
for v in xsumar:
xsum += v

xsum /= len(xsumar)

pixels[offset+rgb] = xsum
#x
for rgb in [0,1,2]:
for y in range(0,h):
#print i," of ",w
for x in range(0,w):

hN = N/2
offset = (y-hN) * stride + (x-hN) * 4
if offset < 0: offset = 0
pixel = pixels[offset+rgb]

xsumar.append(pixel)

if len(xsumar) > N:
xsumar = xsumar[-N:]

xsum = 0
for v in xsumar:
xsum += v

xsum /= len(xsumar)

pixels[offset+rgb] = xsum

-- 
http://mail.python.org/mailman/listinfo/python-list


pure python gaussian blur

2007-08-14 Thread Gerdus van Zyl
Does anyone have a relatively fast gaussian blur implemented in pure
python? Below is my attempt but it takes 2.9 seconds for a 320x240
image. Image comes from byte string: self.array =
array.array('B',srcstring). Would some sort of matrix multiplication
be faster? I don't have experience in that.

I don't want to use PIL or http://filters.sourceforge.net/ to avoid
the 300kb dll just for a simple blur.


def blur(self):
# Convolution Kernels
blurFilter = [[0,1,2,1,0],
  [1,2,4,2,1],
  [2,4,8,4,2],
  [1,2,4,2,1],
  [0,1,2,1,0]]
blurDiv = 48
blurFilterSize = len(blurFilter) #5
blurFilterSize2 = blurFilterSize * blurFilterSize

pixels = self.array

stride = self.width * 4
w = self.width
h = self.height
red = 0
green = 0
blue = 0
sx = 0
ex = 0
sy = 0
ey = 0
ss = 0
f1 = 0
f2 = 0
ff = 0
idx = 0
samplesize = 0

offset1 = blurFilterSize/2
offset2 = blurFilterSize/2+2

clr = array.array('B',[255,255,255,255])
px = array.array('B',[255,255,255,255])

for x in xrange(0,w): #w

sx = x - offset1
ex = x + offset2
if sx < 0: sx = x
if ex > w:  ex = w

for y in xrange(0,h):
sy = y - offset1
ey = y + offset2

if sy < 0: sy = y

if ey > h:  ey = h

samplesize = (ex-sx) * (ey-sy)
if samplesize > 0:
ss = blurFilterSize2 / samplesize
if ss > blurFilterSize - 2:
ss = blurFilterSize - 2
else:
ss = 0


idx = 0
ffx = 0
red = 0
green = 0
blue = 0

for vx in xrange(sx,ex):
for vy in xrange(sy,ey):
offset = vy * stride + vx * 4

px = pixels[offset:offset
+4]

f1 = idx / 5
f2 = (idx - f1) % 5
f1 += ss
if f1 > 4:
f1 = 4

ff = blurFilter[f1]
[f2]
#ff = 1
ffx += ff
red   += px[0] * ff
green += px[1] * ff
blue  += px[2] * ff
idx += 1


if samplesize > 0:
red   = red / ffx
green = green / ffx
blue  = blue / ffx
if red > 255: red = 255
if green > 255: green = 255
if blue > 255: blue = 255

clr[0] = red
clr[1] = green
clr[2] = blue

self.setPixel(x, y, clr)

-- 
http://mail.python.org/mailman/listinfo/python-list


Optimizing numpy

2007-05-12 Thread Gerdus van Zyl
I have the following, that is used to convert pixel data and thus
should be as fast as possible:

b = numpy.ndarray (shape=(w,h,4), dtype=numpy.uint8)

a = numpy.frombuffer(buf, numpy.uint8)
a.shape = (w, h, 3)

b[:,:,0] = a[:,:,2]
b[:,:,1] = a[:,:,1]
b[:,:,2] = a[:,:,0]
b[:,:,3] = 255

Can anyone tell me if there is a faster way? Will making use of
weave.blitz or pyrex help?

Thank You.

-- 
http://mail.python.org/mailman/listinfo/python-list