analitics

Pages

Friday, November 8, 2013

Using python with Image, ImageFilter, ImageChops and custom image filter.

Today I will sow you some filters using python Image, ImageFilter and ImageChops.

I make this default python script to open one image and after will be delete from memory.See the default image:


I make also one python class named TEST_FILTER.

This class will have all data from filter, see filterargs.

The filterargs args it's one matrix and this will be the filter.

The default script ...

import Image
import ImageFilter
import ImageChops

class TEST_FILTER(ImageFilter.BuiltinFilter):
    name="TestFilter"
    filterargs = (3,3),10,0,(1,0,1,0,0,0,1,0,1)

def main ():
    filename = "test-gentoo.jpg"
    image = Image.open(filename);
    del image;
if (__name__ =="__main__"):
   main();

Let's try first filter : TEST_FILTER

import Image
import ImageFilter
import ImageChops

class TEST_FILTER(ImageFilter.BuiltinFilter):
    name="TestFilter"
    filterargs = (3,3),10,0,(1,0,1,0,0,0,1,0,1)

def main ():
    filename = "test-gentoo.jpg"
    image = Image.open(filename);
    image.filter(TEST_FILTER).show();
    del image;
if (__name__ =="__main__"):
   main();

The result will be this:


Now for each filter will change this source code...

    image.filter(TEST_FILTER).show();

...with the new filter source code.

Let's see some filters ...

EMBOSS - filter

    image.filter(ImageFilter.EMBOSS).show();

FIND_EDGES - filter

    image.filter(ImageFilter.FIND_EDGES).show();

BLUR - filter

    image.filter(ImageFilter.BLUR).show();

CONTOUR - filter

    image.filter(ImageFilter.CONTOUR).show();

DETAIL - filter

    image.filter(ImageFilter.DETAIL).show();

EDGE_ENHANCE - filter

    image.filter(ImageFilter.EDGE_ENHANCE).show();

Now image convert with bit 1.

    image.convert("1").show();

Invert image with ImageChops.

    ImageChops.invert(image).show();