python图像去除干扰线
(2018-07-30 13:06:27)分类: python开发 |
# coding = utf-8
f r o m P I L i m p o r t I m a g e
f r o m s q l a l c h e m y . s
q l . e x p r e s s i o n i m p o r t e x c e p
t _
image =
Image.open("C:\\Users\\gg\\Desktop\\python_picture\\pic" + str(url)
+ ".png","wb")
imgry =
image.convert('L') # 转化为灰度图
table =
get_bin_table()
out =
imgry.point(table, '1')
out.save("C:\\Users\\gg\\Desktop\\gray_picture\\pic" + str(url) +
".png")
# threshold
是关于原始图像的颜色识别相关内容
def get_bin_table(threshold=255):
"""
获取灰度转二值的映射table
"""
table =
[]
for i in
range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return
table
# 干扰线特点分析
"""
干扰横线:像素上下位置像素为空白
干扰竖线:像素左右位像素为空白
干扰斜线或干扰点:像素上下左右像素为空白
"""
def plx(data, address):
w =
data.width
h =
data.height
for x in
range(1, w - 1):
if x > 1 and x != w - 2:
# 获取目标像素点左右位置
left = x - 1
right = x + 1
for y in range(1, h - 1):
# 获取目标像素点上下位置
up = y - 1
down = y + 1
if x <= 2 or x >= (w - 2):
data.putpixel((x, y), 255)
elif y <= 2 or y >= (h - 2):
f r o m P I L i m p o r t
f r
# 打开该路径下的图片
def openPicture(url):def get_bin_table(threshold=255):
# 干扰线特点分析
"""
干扰横线:像素上下位置像素为空白
干扰竖线:像素左右位像素为空白
干扰斜线或干扰点:像素上下左右像素为空白
"""
def plx(data, address):