powershell识别图片验证码

通过powershell+tesseract分别来完成图片预处理和识别验证。如图验证码:

add-type -AssemblyName System.Drawing
Function 识别([string]$path)
{
$img = [System.Drawing.Bitmap]::FromFile($path)
for($x = 0; $x -lt $img.Width; $x++)
{
    for ($y = 0; $y -lt $img.Height; $y++)
    {
        $__c = $img.GetPixel($x, $y);
                    #灰度值
        $__tc = ($__c.R + $__c.G + $__c.B) / 3;
                    
        if($__tc -lt 140) #小于阙值 黑色,这里的140我是借助屏幕拾色器取的一个估值。
        {
          $img.SetPixel($x, $y, [System.Drawing.Color]::FromArgb($__c.A, 0, 0, 0))
                        
         }
                    #大于阙值 设置为白色
        Else
         {
           $img.SetPixel($x, $y,[System.Drawing.Color]::FromArgb($__c.A, 255, 255, 255));
         }       
   } 
 }
 $newpath=$path.Substring(0,$path.LastIndexOf('.'))+'-copy1.jpg'
 $img.Save($newpath)
 powershell.exe "tesseract $newpath stdout" #进行识别验证
 Remove-Item -Path $newpath #删除掉预处理的图片
}
      

注释基本都写了。最后调用“识别”函数测试一下,识别还可以的:

 好了,就写到这儿吧。

猜你喜欢

转载自blog.csdn.net/qq_24499417/article/details/88831176