python3 实现验证码图片切割的方法


Posted in Python onDecember 07, 2018

切割前图片

python3 实现验证码图片切割的方法

切割后四个图片

python3 实现验证码图片切割的方法

代码

#coding:utf8
import os
from PIL import Image,ImageDraw,ImageFile
import numpy
import pytesseract
import cv2
import imagehash
import collections
class pictureIdenti:

 #rownum:切割行数;colnum:切割列数;dstpath:图片文件路径;img_name:要切割的图片文件
 def splitimage(self, rownum=1, colnum=4, dstpath="D:\work\python36_crawl\Veriycode",
     img_name="D:\work\python36_crawl\Veriycode\mode_5246.png",):
  img = Image.open(img_name)
  w, h = img.size
  if rownum <= h and colnum <= w:
   print('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode))
   print('开始处理图片切割, 请稍候...')

   s = os.path.split(img_name)
   if dstpath == '':
    dstpath = s[0]
   fn = s[1].split('.')
   basename = fn[0]
   ext = fn[-1]

   num = 1
   rowheight = h // rownum
   colwidth = w // colnum
   file_list = []
   for r in range(rownum):
    index = 0
    for c in range(colnum):
     # (left, upper, right, lower)
     # box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)
     if index<1:
      colwid = colwidth+6
     elif index<2:
      colwid = colwidth + 1
     elif index < 3:
      colwid = colwidth

     box = (c * colwid, r * rowheight, (c + 1) * colwid, (r + 1) * rowheight)
     newfile = os.path.join(dstpath, basename + '_' + str(num) + '.' + ext)
     file_list.append(newfile)
     img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext)
     num = num + 1
     index+=1
   for f in file_list:
    print(f)
   print('图片切割完毕,共生成 %s 张小图片。' % num)

以上这篇python3 实现验证码图片切割的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python的绘图工具matplotlib使用实例
Jul 03 Python
10个易被忽视但应掌握的Python基本用法
Apr 01 Python
python使用wmi模块获取windows下硬盘信息的方法
May 15 Python
python图像处理之反色实现方法
May 30 Python
Python性能提升之延迟初始化
Dec 04 Python
Python网络爬虫中的同步与异步示例详解
Feb 03 Python
Django中Forms的使用代码解析
Feb 10 Python
python 3.6.2 安装配置方法图文教程
Sep 18 Python
python的mysql数据库建立表与插入数据操作示例
Sep 30 Python
python如何写个俄罗斯方块
Nov 06 Python
Python用户自定义异常的实现
Dec 25 Python
Python使用pandas导入csv文件内容的示例代码
Dec 24 Python
python 用opencv调用训练好的模型进行识别的方法
Dec 07 #Python
Python cv2 图像自适应灰度直方图均衡化处理方法
Dec 07 #Python
浅析python3字符串格式化format()函数的简单用法
Dec 07 #Python
Python实现的批量修改文件后缀名操作示例
Dec 07 #Python
Python实现随机创建电话号码的方法示例
Dec 07 #Python
selenium3+python3环境搭建教程图解
Dec 07 #Python
python对视频画框标记后保存的方法
Dec 07 #Python
You might like
哪吒敖丙传:新人物二哥敖乙出场 小敖丙奶气十足
2020/03/08 国漫
中英文字符串翻转函数
2008/12/09 PHP
php中用socket模拟http中post或者get提交数据的示例代码
2013/08/08 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(九)
2014/06/24 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十五)
2014/06/30 PHP
PHP函数import_request_variables()用法分析
2016/04/02 PHP
yii2中LinkPager增加总页数和总记录数的实例
2017/08/28 PHP
IE8 下的Js错误HTML Parsing Error...
2009/08/14 Javascript
你需要知道的10个最佳javascript开发实践小结
2012/04/15 Javascript
javascript数组操作(创建、元素删除、数组的拷贝)
2014/04/07 Javascript
javascript设计模式之解释器模式详解
2014/06/05 Javascript
JS数组(Array)处理函数整理
2014/12/07 Javascript
js获取时间并实现字符串和时间戳之间的转换
2015/01/05 Javascript
JavaScript控制两个列表框listbox左右交换数据的方法
2015/03/18 Javascript
jQuery实现页面顶部显示的进度条效果完整实例
2015/12/09 Javascript
JS实现的跨浏览器解析XML文件实例
2016/06/21 Javascript
Javascript 高性能之递归,迭代,查表法详解及实例
2017/01/08 Javascript
3分钟掌握常用的JS操作JSON方法总结
2017/04/25 Javascript
Angular刷新当前页面的实现方法
2018/11/21 Javascript
浅析 Vue 3.0 的组装式 API(一)
2020/08/31 Javascript
[12:36]《DOTA2》国服注册与激活指南全攻略
2013/04/28 DOTA
python监控网卡流量并使用graphite绘图的示例
2014/04/27 Python
python转换字符串为摩尔斯电码的方法
2015/07/06 Python
Python随机生成带特殊字符的密码
2016/03/02 Python
Python实现自定义函数的5种常见形式分析
2018/06/16 Python
python实现比较文件内容异同
2018/06/22 Python
Python pip替换为阿里源的方法步骤
2019/07/02 Python
python——全排列数的生成方式
2020/02/26 Python
详解Tensorflow不同版本要求与CUDA及CUDNN版本对应关系
2020/08/04 Python
澳大利亚领先的时尚内衣零售商:Bras N Things
2020/07/28 全球购物
银行领导证婚词
2014/01/11 职场文书
最新个人职业生涯规划书
2014/01/22 职场文书
公司开业庆典策划方案
2014/06/04 职场文书
授权委托书样本
2014/09/25 职场文书
大学生村官工作总结2015
2015/04/09 职场文书
MybatisPlus EntityWrapper如何自定义SQL
2022/03/22 Java/Android