1. 程式人生 > 程式設計 >python實現圖片二值化及灰度處理方式

python實現圖片二值化及灰度處理方式

我就廢話不多說了,直接上程式碼吧!

整合環境:win10 pycharm


#!/usr/bin/env python3.5.2
# -*- coding: utf-8 -*-
'''4圖片灰度調整及二值化:
整合環境:win10 python3 Pycharm
'''

from PIL import Image

# load a color image
im = Image.open('picture\\haha.png' )#當前目錄建立picture資料夾

# convert to grey level image
Lim = im.convert('L' )
Lim.save('pice.jpg' )

# setup a converting table with constant threshold
threshold = 185
table = []
for i in range(256):
  if i < threshold:
    table.append(0)
  else:
    table.append(1)

# convert to binary image by the table
bim = Lim.point(table,'1' )

bim.save('picf.png' )

以上這篇python實現圖片二值化及灰度處理方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。