473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

pure python gaussian blur

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)

Aug 14 '07 #1
2 4436
On 8/14/07, Gerdus van Zyl <ge**********@gmail.comwrote:
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.
The one time I tried something similar (implementing a signal
processing function in Python), I found NumPy to be _significantly_
faster -- which makes sense, since all of the matrix arithmetic was
implemented in hard coded, compiled C code instead of interpreted byte
code.

--wpd
Aug 14 '07 #2
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 <dnpsite.narod.ru>
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

Aug 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Allen | last post by:
Hi all you Java 2D gurus out there. I wonder if you might be able to help me. I've been dipping my toe into the world of Gaussian blurring and with the help of a few articles and some bashing my...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
7
by: Robert Dodier | last post by:
Hello, I have an idea, thus far half-baked, to create a language called Ephemeral, for "flexible modeling & reasoning language". I'd like some advice about how to go about implementing it. ...
5
by: Fuzzyman | last post by:
Python 2.4 is built with Microsoft Visiual C++ 7. This means that it uses msvcr7.dll, which *isn't* a standard part of the windows operating system. This means that if you build a windows installer...
2
by: delerious | last post by:
Please take a look at this page: http://home.comcast.net/~delerious1/index10b.html There is javascript code that is executed when the page is loaded. The code calls the focus() method and...
4
by: George Hester | last post by:
http://home.nycap.rr.com/foryorisonly/site.htm The top one is very different from the bottom one. If you do not recognize the difference scroll the images to the right. Not that the toolbar...
7
by: Ragnorack67 | last post by:
Hi, is it possible to change a form text field to this.blur() from a separate function? So, what I want to do is when a button is click it will runblur(), and cause form input name "xyz" to go...
0
by: John Ling | last post by:
Hello, I have been trying to install an application which requires Python: http://chip.dfci.harvard.edu/~wli/MAT/ My environment is AIX 5.2. Below is the section of the setup that has failed,...
12
by: betabrain.honshu | last post by:
Hi Folks, for those of you who are familiar with the micropledge.com project, here is a good opportunity to spend or earn something: http://micropledge.com/projects/pysalsa20 I know that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.