python图片简易二值化

  • 导入相关库
import numpy as np
from PIL import Image
import skimage.io
  • 转化为灰度图
img = Image.open(file_name)
img = img.convert("L")
  • 图片均值
imgs = skimage.io.imread(file_name)
ttt = np.mean(imgs)
  • 二值化
WHITE, BLACK = 255, 0

img = img.point(lambda x: WHITE if x > ttt else BLACK)
img = img.convert('1')
img.save(new_file_name)

你可能感兴趣的:(python)