顶级白P?文字提取器项目超级好用

本文介绍了一种方法来应对网页上无法直接复制的文字。通过使用Python编程和百度OCR API,可以识别并提取图片中的文字,然后显示在文本编辑框中,允许用户进行保存。该过程涉及图像缩放、API调用、结果解析和文本保存等步骤,为无法直接复制文字的场景提供了解决方案。

我们有时在寻找一些文章、资料时,往往会遇到一些无法复制的情况,例如:

 白p的快乐瞬间没有了

那么

如何应对呢?

不嫌麻烦这么干:

 嫌麻烦?那就看这个!

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import requests
import base64


def resize(w, h, newW, newH, pilImage):
    f1 = 1.0 * newW / w
    f2 = 1.0 * newH / h
    factor = min([f1, f2])
    width = int(w * factor)
    height = int(h * factor)
    return pilImage.resize((width, height), Image.ANTIALIAS)


apiKey = 'Vo9OFzDiLi2SNv7rd5L8FDh2'
secretKey = 'caaVMYOdjcgW70c4M8QTNS7cvh0KQDLW'


def getToken():
    getTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + apiKey + '&client_secret=' + secretKey
    response = requests.get(getTokenUrl)
    data = response.json()
    token = data.get('access_token')
    return token


def getResult(imagePath):
    url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage'
    # 设置图片编码
    with open(imagePath, 'rb') as f:
        image = f.read()
    b64Image = base64.b64encode(image)
    # 设置请求参数
    params = {'access_token': getToken()}
    data = {'image': b64Image}
    # 发送网络请求
    response = requests.post(url, params=params, data=data)
    content = response.json()
    result = content['words_result']
    print(result)
    # 将内容写入文件
    for i in result:
        with open("text.txt", 'a', encoding='utf-8') as f:
            f.write(i['words'] + '\n')
    # 界面跳转
    window.destroy()
    newWindow = tk.Tk()
    newWindow.resizable(0, 0)
    newWindow.title('文本编辑框')
    bg = ImageTk.PhotoImage(file="images/bg2.jpg")
    bgLabel = tk.Label(newWindow, width=1000, height=600, image=bg)
    bgLabel.pack()

    with open("text.txt", 'r', encoding='utf-8')as f:
        words = f.read()
    text = tk.Text(newWindow, width=56, height=16, font=("font/simhei.ttf", 18))
    text.place(x=162, y=85)
    text.insert(tk.INSERT, words)

    # 添加保存按钮
    saveImg = ImageTk.PhotoImage(file="images/save.png")
    saveBtn = tk.Button(newWindow, image=saveImg, width=163, height=59, bd=0, command=lambda: save(text))
    saveBtn.place(x=415, y=495)
    newWindow.mainloop()

def save(text):
    content = text.get('1.0', tk.END)
    with open("text.txt", 'w', encoding='utf-8')as f:
        f.write(content)


def chooseImage():
    global imagePath
    imagePath = filedialog.askopenfilename(initialdir="./img", title='Choose an image.')
    imageFile = Image.open(imagePath)
    w, h = imageFile.size
    # 绘制选择的图片
    scaleImg = resize(w, h, 450, 290, imageFile)
    chooseImg = ImageTk.PhotoImage(scaleImg)
    chooseLab = tk.Label(window, image=chooseImg, width=455, height=285)
    chooseLab.place(x=100, y=155)
    window.mainloop()


window = tk.Tk()
window.resizable(0, 0)
window.title('文字提取器')
bg = ImageTk.PhotoImage(file="images/bg.jpg")
bgLabel = tk.Label(window, width=1000, height=600, image=bg)
bgLabel.pack()
# 选择图片按钮
chooseImg = ImageTk.PhotoImage(file="images/choose.png")
choose = tk.Button(window, image=chooseImg, width=242, height=71, bd=0, command=chooseImage)
choose.place(x=635, y=185)
# 识别文字按钮
startImg = ImageTk.PhotoImage(file="images/start.png")
start = tk.Button(window, image=startImg, width=242, height=72, bd=0, command=lambda: getResult(imagePath))
start.place(x=635, y=350)
window.mainloop()

图片给大家放在这里:

bg.jpg:

 bg2.jpg:

choose.png:

 

save.png:

 

 start.png:

记住,一定要把这些图片放在images文件夹里!!! 

最终效果:

 

 

 

素材及源代码来自达内教育 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值