Pure python Integrated Image Processing gadget (3) 10 filter algorithms

Source: Internet
Author: User

< background >

Filter processing is a very common method in image processing. For example, the filter effect in Photoshop, in addition to the filter itself, but also extended a lot of third-party filter effect plug-ins, can be a rich variety of images to transform, many mobile apps to implement the real-time filter function, the most famous is the most popular Instagram.

The principle of the filter, the common is for the digital image of the pixel matrix, using a nxn square matrix filter (ie, kernel, common such as 3x3,5x5, etc.), the pixel matrix traversal, the image after the traversal is the output image, if the algorithm is optimized, the speed of the traverse is fast enough, That's real-time filter (live filter), which lets you preview the effect of image filtering in real time.

ImageFilter is the Python PIL filter module, the current version supports 10 kinds of enhanced filters, through these predefined filters, you can easily do some filtering on the picture, so as to remove the noise in the picture (partial elimination), which can reduce the complexity of the image processing algorithm ( such as pattern recognition, etc.), more convenient to implement and preview the effect of some algorithms.

This script contains all of the following filters to achieve preview of the effects of 10 image processing filters and to save the JPEG file.

Imagefilter.blur

Blur Filter
Imagefilter.contour
Contour
Imagefilter.detail
Detail Filter
Imagefilter.edge_enhance Border strengthening
Imagefilter.edge_enhance_more Boundary Enhancement (valve value is large)
Imagefilter.emboss Embossed Filter
Imagefilter.find_edges Boundary filter
Imagefilter.smooth Smoothing Filters
Imagefilter.smooth_more Smoothing filter (large valve value)
Imagefilter.sharpen Sharpen filter

< effects >

Original:

Blur Filter:

Sharpness Enhancement Filter:

Detail filter:

Contour Filter:

Boundary Extraction Filters:

Boundary Enhancement Filters:

Boundary Enhancement Filter-enhanced version:

Smoothing Filters:

Smoothing Filters-Enhanced version:

Embossed Filter:

< source analysis >

the filter algorithm for the PIL library can be The Python\lib\site-packages\pil path is found below, as shown below:

 

Under the PIL path, we see three files with the same name but different suffixes: imagefilter.py imagefilter.pyc imagefilter.pyo.

. pyc file : is a. py compiled bytecode file for the interpreter to interpret and execute;

. pyo file : is an optimized bytecode file for. pyc files with the same name, usually smaller and faster to run.

The filter algorithm is in the imagefilter.py file.

As mentioned earlier, each filter usually corresponds to a filter (i.e., kernel), the kernel in PiL are common 3x3 and 5x5 square matrices, the following are the matrices for the 9 filters in PiL:

Blur Filter:

classBLUR (Builtinfilter):
Name ="Blur"
Filterargs = (5, 5), 16, 0, (
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
)

Contour Filter:

classCONTOUR (Builtinfilter):
Name ="Contour"
Filterargs = (3, 3), 1, 255, (
-1,-1,-1,
-1, 8,-1,
-1,-1,-1
)

Detail filter:

classDETAIL (Builtinfilter):
Name ="Detail"
Filterargs = (3, 3), 6, 0, (
0,-1, 0,
-1, 10,-1,
0,-1, 0
)

Edge Enhancement Filters:

classEdge_enhance (Builtinfilter):
Name ="edge-enhance"
Filterargs = (3, 3), 2, 0, (
-1,-1,-1,
-1, 10,-1,
-1,-1,-1
)

Edge Enhancement Filter-enhanced version:

The enhanced version and the original filter are only one parameter size of the Matrix 2 row 2 column, which actually modifies the weight of the center pixel. This value can be arbitrarily modified to customize the amplitude of edge enhancement.classEdge_enhance_more (Builtinfilter):
Name ="edge-enhance More"
Filterargs = (3, 3), 1, 0, (
-1,-1,-1,
-1, 9,-1,
-1,-1,-1
)

Embossed Filter

classEMBOSS (Builtinfilter):
Name ="Emboss"
Filterargs = (3, 3), 1, 128, (
-1, 0, 0,
0, 1, 0,
0, 0, 0
)


Edge Extraction Filter

classFind_edges (Builtinfilter):
Name ="Find Edges"
Filterargs = (3, 3), 1, 0, (
-1,-1,-1,
-1, 8,-1,
-1,-1,-1
)

Smoothing Filters:

classSMOOTH (Builtinfilter):
Name ="Smooth"
Filterargs = (3, 3), 13, 0, (
1, 1, 1,
1, 5, 1,
1, 1, 1

Smoothing Filters-Enhanced version:

The enhancement of the smoothing filter is to increase the size of the filter window, with a 3x3 extension to 5x5, so that each new pixel is produced with a weighted contribution of 25 surrounding original pixels (the closer you are, the greater the contribution), the more smooth and natural the result will be, the more slowly the processing speed will become.  

classSmooth_more (Builtinfilter):
Name ="Smooth More"
Filterargs = (5, 5), 100, 0, (
1, 1, 1, 1, 1,
1, 5, 5, 5, 1,
1, 5, 44, 5, 1,
1, 5, 5, 5, 1,
1, 1, 1, 1, 1
)


Sharpening filters:

classSharpen (Builtinfilter):
Name ="Sharpen"
Filterargs = (3, 3), 16, 0, (
-2,-2,-2,
-2, 32,-2,
-2,-2,-2
)

< script source >

#Start
#-*-coding:cp936-*-
ImportImage,imagedraw
ImportImagefilter,random,sys
img = Image.open ("1.jpg")

##图像处理 # #

#convert to RGB image
img = Img.convert ("RGB")

#after the PIL comes with filter processing
Imgfilted_b = Img.filter (Imagefilter.blur)
Imgfilted_c = Img.filter (imagefilter.contour)
Imgfilted_ee = Img.filter (imagefilter.edge_enhance)
Imgfilted_ee_m = Img.filter (Imagefilter.edge_enhance_more)
Imgfilted_em = Img.filter (Imagefilter.emboss)
Imgfilted_fe = Img.filter (imagefilter.find_edges)
IMGFILTED_SM = Img.filter (Imagefilter.smooth)
Imgfilted_sm_m = Img.filter (Imagefilter.smooth_more)
Imgfilted_sh = Img.filter (Imagefilter.sharpen)
Imgfilted_d = Img.filter (imagefilter.detail)

##图像保存 # #

Imgfilted_b.save ("1b.jpg")
Imgfilted_c.save ("1c.jpg")
Imgfilted_ee.save ("1ee.jpg")
Imgfilted_ee_m.save ("1eem.jpg")
Imgfilted_em.save ("1em.jpg")
Imgfilted_fe.save ("1fe.jpg")
Imgfilted_sm.save ("1sm.jpg")
Imgfilted_sm_m.save ("1smm.jpg")
Imgfilted_sh.save ("1sh.jpg")
Imgfilted_d.save ("1d.jpg")

##图像显示 # #

Imgfilted_b.show ()
Imgfilted_c.show ()
Imgfilted_ee.show ()
Imgfilted_ee_m.show ()
Imgfilted_em.show ()
Imgfilted_fe.show ()
Imgfilted_sm.show ()
Imgfilted_sm_m.show ()
Imgfilted_sh.show ()
Imgfilted_d.show ()
#End


Pure python Integrated Image Processing gadget (3) 10 filter algorithms

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.