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

白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文件夹里!!!
最终效果:

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

2737

被折叠的 条评论
为什么被折叠?



