python - 图片灰度化、二值化

image

1. 灰度化

from PIL import Image
img = Image.open("test.jpg")
img = img.convert("L")
img.save("output.jpg")

image

2. 二值化

from PIL import Image
img = Image.open("test.jpg")
img = img.convert("L")
threshold = 180
table = [x > threshold for x in range(256)]
img = img.point(table, "1")
img.save("output.jpg")

image

posted @ 2024-04-02 20:38  wstong  阅读(96)  评论(0编辑  收藏  举报