欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中使用PIL庫(kù)實(shí)現(xiàn)圖片高斯模糊實(shí)例

 更新時(shí)間:2015年02月08日 11:20:18   投稿:junjie  
這篇文章主要介紹了Python中使用PIL庫(kù)實(shí)現(xiàn)圖片高斯模糊實(shí)例,本文重點(diǎn)在修改了Pil的源碼實(shí)現(xiàn)可以自定義模糊度,需要的朋友可以參考下

一、安裝PIL

PIL是Python Imaging Library簡(jiǎn)稱,用于處理圖片。PIL中已經(jīng)有圖片高斯模糊處理類,但有個(gè)bug(目前最新的1.1.7bug還存在),就是模糊半徑寫死的是2,不能設(shè)置。在源碼ImageFilter.py的第160行:

所以,我們?cè)谶@里自己改一下就OK了。

項(xiàng)目地址:http://www.pythonware.com/products/pil/

二、修改后的代碼

代碼如下:

復(fù)制代碼 代碼如下:

#-*- coding: utf-8 -*-

from PIL import Image, ImageFilter

class MyGaussianBlur(ImageFilter.Filter):
    name = "GaussianBlur"

    def __init__(self, radius=2, bounds=None):
        self.radius = radius
        self.bounds = bounds

    def filter(self, image):
        if self.bounds:
            clips = image.crop(self.bounds).gaussian_blur(self.radius)
            image.paste(clips, self.bounds)
            return image
        else:
            return image.gaussian_blur(self.radius)

三、調(diào)用

復(fù)制代碼 代碼如下:

simg = 'demo.jpg'
dimg = 'demo_blur.jpg'
image = Image.open(simg)
image = image.filter(MyGaussianBlur(radius=30))
image.save(dimg)
print dimg, 'success'

如果只需要處理某個(gè)區(qū)域,傳入bounds參數(shù)即可

四、效果
原圖:

處理后的:

相關(guān)文章

最新評(píng)論