Skip to content

Instantly share code, notes, and snippets.

@asukaminato0721
Last active March 7, 2022 09:34
Show Gist options
  • Save asukaminato0721/d39be36fba365ec93115cbf7a7f417b4 to your computer and use it in GitHub Desktop.
Save asukaminato0721/d39be36fba365ec93115cbf7a7f417b4 to your computer and use it in GitHub Desktop.
needs improve for deal with picture.
#!/usr/bin/env python3
# use pillow https://pillow.readthedocs.io/en/latest/reference/ImageGrab.html to copy a place of screen, get pillow object
# use tesseract to transform a picture into text
# use TextBlob to translate text
# use google translate to translate text
from time import sleep
from typing import Tuple
import pytesseract
from PIL import Image, ImageGrab
from textblob import TextBlob
Position = Tuple[int, int]
def get_text_frame() -> Tuple[Position, Position]:
"""
https://www.jianshu.com/p/8e508c6a05ce
"""
import pyautogui as pag
print("wait 2s")
sleep(2)
p1 = pag.position()
print("茄子")
print("p1:", p1)
print("wait 2s")
sleep(2)
p2 = pag.position()
print("茄子")
print("p2:", p2)
return p1, p2
def ocr(img: Image.Image) -> str:
# TODO: improve the picture for ocr.
img = img.convert("L")
threshold = 200
table = [0] * threshold + [1] * (256 - threshold)
img = img.point(table, "1")
# img.show()
return (
pytesseract.image_to_string(img, lang="jpn")
.replace("\n", "")
.replace(" ", "")
)
def capture_picture_from_coordinate(x: Position, y: Position):
im = ImageGrab.grab(bbox=(*x, *y))
return im
def translate_from_jp_to_zh(s: str):
"""
>>> translate_from_jp_to_zh("おはよう")
>>> 早上好
"""
tb = TextBlob(s)
try:
translated = tb.translate(from_lang="ja", to="zh").strip()
except:
translated = "error"
return str(translated)
def main():
p1, p2 = get_text_frame()
init = Image.new(mode="RGB", size=(1, 1))
ja_text = ""
zh_text = ""
while True:
sleep(2)
new_img = capture_picture_from_coordinate(x=p1, y=p2)
if new_img != init:
ja_text = ocr(new_img)
print(ja_text)
zh_text = translate_from_jp_to_zh(ja_text)
print("translate: ", zh_text)
init = new_img
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment