近期接的一个需求需要去除图片的红章,用到了PIL库。
from PIL import Image,ImageEnhance
import os
#f="5-12 - 0001.tif"
for rt, dirs, files in os.walk("D:\工作文件夹\OCR\pictest"):
for f in files:
fname = os.path.splitext(f)
newName = fname[0] + '_nostamp' + fname[1]
im=Image.open(os.path.join(rt,f))
im=ImageEnhance.Contrast (im).enhance(1.5)
im=ImageEnhance.Sharpness(im).enhance(1.5)
w,h=im.size
print("PicSize:w=%d,h=%d.Processing..." %(w,h))
pixels = im.load()
if w>=h:
p,q=w//3,h//3
else:
p,q=w//2,h//4
for x in range(p):
for y in range(q):
if pixels[x,y][0]>=150:
pixels[x,y]= (255,255,255)
im.save("D:\工作文件夹\OCR\picout\\"+newName)
#im.show()