使用python3怎么切割验证码图片

这期内容当中小编将会给大家带来有关使用python3怎么切割验证码图片,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

python是什么意思

Python是一种跨平台的、具有解释性、编译性、互动性和面向对象的脚本语言,其最初的设计是用于编写自动化脚本,随着版本的不断更新和新功能的添加,常用于用于开发独立的项目和大型项目。

切割前图片

使用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怎么切割验证码图片了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。