Kindleの文字起こしするコードを書いた。
中身は手作業でやっている作業をPythonで自動化したものだ。
キーボードでページを送り、画面コピーを行う。
あっという間に、本一冊分のJPG化が完了する。
import pyautogui
from PIL import ImageGrab
import timepos =(875,1070)
page = 300pyautogui.click(pos, button="left")
for i in range(page):
pyautogui.click(500, 500,button="left")
pyautogui.press("right")
time.sleep(0.5)
screenshot = ImageGrab.grab()
screenshot.save('{}screenshot.jpg'.format(i))
お次は画像からの文字起こし。
すこし余計な部分はトリミングする。
import cv2
import pytesseract
from pytesseract import Output
import os
import PIL.Image as im
page = 248for i in range(page):
fn = '{}screenshot.jpg'.format(i)
is_file = os.path.isfile(fn)
if is_file:
img = im.open(fn)
tr = img.crop((250,100,830,970))
tr.save('output/'+fn)
OCRをインストール
Tesseract OCR in Python with Pytesseract & OpenCV
ひとまず、OCRでテキストを抜き取って、最後はまとめて排出した。
import cv2
import pytesseract
from pytesseract import Output
import os
import PIL.Image as im
page = 203
all = ""for i in range(page):
fn = '{}screenshot.jpg'.format(i)
is_file = os.path.isfile('output/'+fn)
if is_file:
img = cv2.imread('output/'+fn)
print(fn)
img = im.open('output/'+fn)
custom_config = r'--psm 3'
e = pytesseract.image_to_string(img, config=custom_config, lang='eng')
e=" ".join(e.split('\n'))
all = all + e
else:
pass