https://blog.csdn.net/column/details/pythonpil.html
示例:
from PIL import Image import pytesseract pytesseract.pytesseract.tesseract_cmd = r'D:Program FilesTesseract-OCR esseract.exe' im = Image.open(r'a.jpg') print(im) print(im.mode) print(im.getpixel((100,100))) gray = im.convert('L') gray.save('ag.jpg') print(gray) print(gray.mode) print(gray.getpixel((100,100))) # 所有比阈值亮的像素转换为白色,所有比阈值暗的像素转换为黑色 # 灰度 白色为255,黑色为0 b = gray.point(lambda x:0 if x<200 else 255,'1') b.save('b.jpg')
输出:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x291 at 0x16328A31438> RGB (255, 226, 215) <PIL.Image.Image image mode=L size=500x291 at 0x16328A313C8> L 233
a.jpg
ag.jpg
b.jpg