admin管理员组

文章数量:1130349

最近在写毕业论文,爬取数据但是遇到了cannot identify image file错误

#有关的一段代码
import requests
import json
import urllib.request
import pytesseract#OCR识别库
from PIL import Image
# 发送GET请求下载验证码图片
captcha_response = requests.get(url, headers=headers, cookies=cookie)
 
if captcha_response.status_code == 200:
    with open('captcha.png', 'wb') as f:
        f.write(captcha_response.content)
        print("下载成功")
        try:
            image = Image.open('D:\\pycharm项目\\demo\\captcha.png')
            img = image.convert('L')#灰度处理
            # 使用pytesseract进行OCR处理
            text = pytesseract.image_to_string(img)
            print(text)
        except Exception as e:
            print("发生错误:", e)#打印错误信息
 
 
else:
    print("下载失败,状态码为", captcha_response.status_code)

模拟登陆的时候遇到了验证码,将验证码的地址赋值然后直接进行灰度处理进行读取

然后运行遇到了

登录失败,HTTP状态码:200
下载成功
发生错误: cannot identify image file 'D:\\pycharm项目\\demo\\captcha.png'

来个大佬help我

最近在写毕业论文,爬取数据但是遇到了cannot identify image file错误

#有关的一段代码
import requests
import json
import urllib.request
import pytesseract#OCR识别库
from PIL import Image
# 发送GET请求下载验证码图片
captcha_response = requests.get(url, headers=headers, cookies=cookie)
 
if captcha_response.status_code == 200:
    with open('captcha.png', 'wb') as f:
        f.write(captcha_response.content)
        print("下载成功")
        try:
            image = Image.open('D:\\pycharm项目\\demo\\captcha.png')
            img = image.convert('L')#灰度处理
            # 使用pytesseract进行OCR处理
            text = pytesseract.image_to_string(img)
            print(text)
        except Exception as e:
            print("发生错误:", e)#打印错误信息
 
 
else:
    print("下载失败,状态码为", captcha_response.status_code)

模拟登陆的时候遇到了验证码,将验证码的地址赋值然后直接进行灰度处理进行读取

然后运行遇到了

登录失败,HTTP状态码:200
下载成功
发生错误: cannot identify image file 'D:\\pycharm项目\\demo\\captcha.png'

来个大佬help我

本文标签: IdentifyImagefile