python - 圖片灰度化、二值化

wstong發表於2024-04-02

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

相關文章