4 Star 0 Fork 0

hackerTeam2019 / tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lyp_cv_converter.py 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
李乙平 提交于 2019-11-02 19:27 . add tool for cv to black-white
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')
Python
1
https://gitee.com/hackerTeam2019/tools.git
git@gitee.com:hackerTeam2019/tools.git
hackerTeam2019
tools
tools
master

搜索帮助