批量将读取图片进行二值化处理,并以txt形式保存在指定文件夹中

批量将读取图片并进行二值化处理,并以txt形式保存在指定文件夹


#代码


import os
from PIL import Image

def init(fileList,n):
     for i in range(0,n):
          img = Image.open(imagepath + str(fileList[i]))
          w,h=img.size
          img = img.convert('1')
          list_img_name = []
          path_imgs = imagepath
          for files in os.listdir(path_imgs):
               img_path = files.rstrip('.jpg')
               list_img_name.append(img_path)
          with open(txtpath+str(list_img_name[i])+".txt", "w") as f:
               for c in range(h):
                    for j in range(w):
                         f.write(str(int((255-(img.getpixel((j,c))))/255)))
                         if j==w-1:
                              f.write("\n")
          f.close()
if __name__=="__main__":
     fileList=os.listdir(imagepath)
     n=len(fileList)
     init(fileList,n)

#注释

#其中imagepath为读取的图片路径,txtpath保存txt文件的路径。`在这里插入代码片`由于PIL只支持python2版本,若不能运行,安装pillow库可同样适用。
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值