[SciPy-user] PIL and gaussian_filter?

Angus McMorland amcmorl at gmail.com
Wed May 21 10:12:52 EDT 2008


2008/5/21 Johannes Strömberg <johannes.stromberg at gmail.com>:
> Hi,
>
> I am new to using SciPy and I want to use it to apply gaussian
> smoothing/blur to images I get from PIL (Python Imaging Library).
>
> When I use the asarray() method on my PIL image I get a 3-dimensional
> array, shape is  (w, h, 3 [rgb-values]).
>
> I am wondering how I can transform this to something that is
> compatible with f.e. ndimage.gaussian_filter? (When I run it directly
> it simply removes color from the image).

I believe this is because you are also blurring across the last
dimension (i.e. smoothing the colour information for each pixel),
resulting in something much closer to grey in each case. To prevent
this you need to blur only in the first two (spatial) dimensions of
the image:

Here's a one liner that should do what you want:

ar2 = np.concatenate([scipy.ndimage.gaussian_filter(ar[...,x,np.newaxis],
np.std(ar)/k) for x in xrange(ar.shape[2])], axis=2)

where k determines the level of filtering.

It uses a list comprehension over the colour dimension, and there's
probably a better way to do this using np.apply_along_axis or
apply_over_axes, but I can't immediately work it out. Anyone more
familiar with these want to chip that in?

HTH.

Regards,

Angus.
-- 
AJC McMorland, PhD candidate
Physiology, University of Auckland

(Nearly) post-doctoral research fellow
Neurobiology, University of Pittsburgh



More information about the SciPy-User mailing list