代码拉取完成,页面将自动刷新
import os
# -*- coding: utf-8 -*-
import cv2
# 图片二值化
from PIL import Image
def converter(input_path):
img_paths = os.listdir(input_path)
out_path = input_path + '\\out'
if not os.path.exists(out_path):
os.mkdir(out_path)
for imgpath in img_paths:
if os.path.isdir(input_path +'\\' + imgpath):
continue
print('file: ', imgpath)
imgpath = input_path + '\\' + imgpath
print('open file: ', imgpath)
img = Image.open(imgpath)
out = out_path + imgpath[len(input_path):imgpath.index('.jpg')] + '_out.jpg'
print('out: ', out)
# 模式L”为灰色图像,它的每个像素用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度。
Img = img.convert('L')
Img.save("a1.jpg")
# 自定义灰度界限,大于这个值为黑色,小于这个值为白色
threshold = 200
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
# 图片二值化
photo = Img.point(table, '1')
photo.save(out)
converter(r'C:\Users\xiaoyu\img_test')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。