python识别验证码+灰度图片base64转换图片

发布时间:2024年01月04日

一、为后面识别验证码准备

1、图片base64转换为

在这里插入图片描述
在这里插入图片描述

上文中的base64,后面的就是包含Base64编码的PNG图像的字符串复制下来

import base64
from PIL import Image
import io

# 这里是你的Base64编码的字符串
base64_data = "iVBORw0KGgoAAAANSUhEUgAAAG8AAAAkCAIAAAAIOPOYAAAJ1ElEQVR4Xu2Ze1DU1xXHnaRN2qZNG/ucdtrGmU6amU5n0rE2sc3Y2PhH2owa2ulk2oxmMlPZJCYxTNSWRiPiAzWJaKGpCgiiCIjEiKggjwWiJbyRRXmzLLjIG0FeK7Cn37P3x/399u6CW1zyF98549y9v3sv3s+ee865v11A8/KfFqgd87oHzdP0p+Zp+lPzNP2pmWg6ndTTQ3V1dPUqXbtGViv193Pn5y4n9VVT6wXqLKSRTvXh3Atb7uyk69fJYqGWFhofVwdITUuzoIC2bqXAQNXWr6dt2ygxkTo61Cn+1/gQVYTRyUfp8ALdkh6j8p001qcOngMNDdHZsxQc7EbgjTcoPp4dy1NeaE5OUlycClEu9M47Wvutt6itTZ3rT/VWUfJP3TgaLf671JapTvGrqqtp0yaVgLS336aaGnWKF5oZGdqEXbuoooLnYN2yMiospMFB/k7gmPBQDPjjJloQRov+Q6aLZL2lrnNPGmiiY99malFfoAITdfyXHLfYVbuKqSCQjtznevRFas9XJ/pJONevvsp7fP11OnmSAx3CXVUV08jKoqAgzbcUf1Jp3rpFb77JQz/8kCEqTo4vBEAzm2nta1pPvZXKbtKmXHoknPv9I+cknX6CeUV/idoucc9oN9lz6GaBdsDtZop9mAeA+O1W98l+0PAw7xS7w0EEhIQEzXuERUZSba02ICSEJib0iSrNCxe0LwThVkxQrKCCFu4nk0n7eOqUNjHPxkD946G1R7XjXB3BHyv3MVbRA3/Me4UcA+yVcFv0nHmSJh3qCi4NOYbqe+tL7aWXbZfzW/LzrHlmq1kxdNZ0qyf20iXeGvaI46j4k7CdOyk7W2vn5OgTVZoYhxGHDnEW8lyF/0YybZ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nAd6wmxz9lLFKP/hNhhdZPgslOupKS0BA6eLFltWrQXa6A26UeOOZk6P9aqQQ8JWmqDc9zYf/wGyFyjxjJcPCAcfpxqkHNctBMr9MMQ/pKAs3qhPnUlu28K537dIJWK36U19pwp/FQkZDKB8dVUf6VU6yHKDYb+jsjIYbfVmoOmOOJX43kxYczLleyleaUHc3v98MCdHu7DBvhcQc6M4g1RyhzBfo5CL+uQ2uikb+On5z/LnL6eRfOEJDufAEykbjG4b/i+a87qp5mv7UPE1/ap6mP/U/Jy5/g8V/0ucAAAAASUVORK5CYII="

# 解码Base64数据为二进制
image_data = base64.b64decode(base64_data)

# 将二进制数据转换为图片
with Image.open(io.BytesIO(image_data)) as img:
	# img.show() image展示
	# 保存图片到本地
	img.save("decoded_image.png", "PNG")
	# 变成灰色图
	gray_img = img.convert('L')
    gray_img.show()

2、二值化图片

import base64

from PIL import Image
import io

# 这里是你的Base64编码的字符串
base64_data = "iVBORw0KGgoAAAANSUhEUgAAAG8AAAAkCAIAAAAIOPOYAAAJ1ElEQVR4Xu2Ze1DU1xXHnaRN2qZNG/ucdtrGmU6amU5n0rE2sc3Y2PhH2owa2ulk2oxmMlPZJCYxTNSWRiPiAzWJaKGpCgiiCIjEiKggjwWiJbyRRXmzLLjIG0FeK7Cn37P3x/399u6CW1zyF98549y9v3sv3s+ee865v11A8/KfFqgd87oHzdP0p+Zp+lPzNP2pmWg6ndTTQ3V1dPUqXbtGViv193Pn5y4n9VVT6wXqLKSRTvXh3Atb7uyk69fJYqGWFhofVwdITUuzoIC2bqXAQNXWr6dt2ygxkTo61Cn+1/gQVYTRyUfp8ALdkh6j8p001qcOngMNDdHZsxQc7EbgjTcoPp4dy1NeaE5OUlycClEu9M47Wvutt6itTZ3rT/VWUfJP3TgaLf671JapTvGrqqtp0yaVgLS336aaGnWKF5oZGdqEXbuoooLnYN2yMiospMFB/k7gmPBQDPjjJloQRov+Q6aLZL2lrnNPGmiiY99malFfoAITdfyXHLfYVbuKqSCQjtznevR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IdROGzfqGxeWkuI2xleaolhF4UpzTRPK/xsDgleyPzrp0p90BxSGXC82n75C68n5i7oILk0jvevS1q09sza5OrnoRpF90A7E3H/xIoohdbRLY3Z75YoVY9P8qIAbpPHXHVxtlGrKV5pivqgcjDQrK9WRftCgVavhRSqfdLAPAi6K+ePfo9w12qEeaNJewsMaEtyXmEko0VFXohhS+sdu3KgOCOg67Sp1p5GxQgoKmm3cFPMRMnC/fP99fUWLRR3pH4kjfOybNHRDfaTJSeeWayhP/IAmxtTnMwolOoIjTvRIQwM+jlqt7VFRlc8+24WbyYwS2Vja+vV023CR85WmeIfkaZ6/NPlHOOMX/8Ckkh+nAd6wmxz9lLFKP/hNhhdZPgslOupKS0BA6eLFltWrQXa6A26UeOOZk6P9aqQQ8JWmqDc9zYf/wGyFyjxjJcPCAcfpxqkHNctBMr9MMQ/pKAs3qhPnUlu28K537dIJWK36U19pwp/FQkZDKB8dVUf6VU6yHKDYb+jsjIYbfVmoOmOOJX43kxYczLleyleaUHc3v98MCdHu7DBvhcQc6M4g1RyhzBfo5CL+uQ2uikb+On5z/LnL6eRfOEJDufAEykbjG4b/i+a87qp5mv7UPE1/ap6mP/U/Jy5/g8V/0ucAAAAASUVORK5CYII="

# 解码Base64数据为二进制
image_data = base64.b64decode(base64_data)

# 将二进制数据转换为图片
with Image.open(io.BytesIO(image_data)) as im:
    img = im.convert('L')
    pixels = img.load()
    for x in range(img.width):
        for y in range(img.height):
            # 可以适当调大这个200
            if pixels[x, y] > 200:
                pixels[x, y] = 255
            else:
                pixels[x, y] = 0
    img.show()

二、识别图片上内容(ddddocr库)

由于本人python版本问题pip出现了 就不进行验证了
在这里插入图片描述
链接

文章来源:https://blog.csdn.net/weixin_49390750/article/details/135386283
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。