#首先导入Image、 from PIL import Image #1、打开图像 image=Image.open('C:/1.png') #2、显示图片 image.show() #3、图像转化为灰度,参数用'L',转化为阈值为127的黑白图像,参数用'1' image.convert('L') #4、图像转化为自定义阈值的图像,需要先转化为灰度或者黑白,再执行point threshold = 150 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) image.convert('L').point(table,'1') #5、裁剪图像,裁剪区域用左上顶点和右下顶点坐标圈定 #region=(left,uper,right,lower) region=(1,1,79,17) image.crop(region) #6、图像保存 image.save('C:/2.png')