在Python中使用PIL模組對圖片進行高斯模糊處理的教程

來源:互聯網
上載者:User
從一篇文章中看到,PIL 1.1.5 已經內建了高斯模糊,但是並沒有在文檔中提及,而且PIL的高斯模糊中 radius 是寫入程式碼, 雖然構造方法中有傳入 radius 參數,但壓根就沒有用到 (看這裡),所以需要自己進行改造,當然,知道了原因, 修改起來自然非常簡單了。

結合文章中的需求,對局部進行高斯模糊,所以還需要結合使用 crop 和 paste 方法實現局部使用濾鏡。

代碼如下:

#-*- coding: utf-8 -*-from PIL import Image, ImageFilterclass 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)bounds = (150, 130, 280, 230)image = Image.open('source.jpg')image = image.filter(MyGaussianBlur(radius=29, bounds=bounds))image.show()

可以看下效果:

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.