Пример #1
0
def ocr_word(mouse_x, mouse_y):
    # Return None if occur unsupported language.
    src_lang = setting_config.get_translate_config("src_lang")
    if not LANGUAGE_OCR_DICT.has_key(src_lang):
        show_message("对不起, 屏幕取词当前还不支持%s" % _(src_lang), "取消", "我知道了", lambda : ocr_log(src_lang))
        return None

    # Return None if found any ocr package need install before continue. 
    ocr_pkg_name = LANGUAGE_OCR_DICT[src_lang]
    pkg_names = get_install_packages([ocr_pkg_name])
    if len(pkg_names):
        show_message("需要安装OCR语言包以启用翻译功能", "取消", "安装", lambda : install_packages(pkg_names))
        return None
    
    # Return None if mouse at trayicon area.
    if constant.TRAYAREA_TOP < mouse_y < constant.TRAYAREA_BOTTOM:
        return None

    # Ocr word under cursor.
    lang = ocr_pkg_name.split("tesseract-ocr-")[1]
    x = max(mouse_x - screenshot_width / 2, 0) 
    y = max(mouse_y - screenshot_height / 2, 0)
    width = min(mouse_x + screenshot_width / 2, screen_width) - x
    height = min(mouse_y + screenshot_height / 2, screen_height) - y
                    
    scale = 2
    tool = pyocr.get_available_tools()[0]
        
    output_format = xcb.xproto.ImageFormat.ZPixmap
    plane_mask = 2**32 - 1
    
    reply = conn.core.GetImage(
        output_format, 
        root, 
        x,
        y,
        width,
        height,
        plane_mask).reply()
    image_data = reply.data.buf()
    image = Image.frombuffer("RGBX", (width, height), image_data, "raw", "BGRX").convert("RGB")
    
    word_boxes = tool.image_to_string(
        image.convert("L").resize((width * scale, height * scale)),
        lang=lang,
        builder=pyocr.builders.WordBoxBuilder())
    
    cursor_x = (mouse_x - x) * scale
    cursor_y = (mouse_y - y) * scale
    
    for word_box in word_boxes[::-1]:
        ((left_x, left_y), (right_x, right_y)) = word_box.position
        if (left_x <= cursor_x <= right_x and left_y <= cursor_y <= right_y):
            word = filter_punctuation(word_box.content)
            # Return None if ocr word is space string.
            if word.isspace():
                return None
            else:
                return word
        
    return None    
Пример #2
0
def ocr_word(mouse_x, mouse_y):
    # Return None if occur unsupported language.
    src_lang = setting_config.get_translate_config("src_lang")
    if not LANGUAGE_OCR_DICT.has_key(src_lang):
        show_message(_("Sorry, select-and-translate does not support %s yet") % _(src_lang), _("Cancel"), _("Ok, I understand"), lambda : ocr_log(src_lang))
        return None

    # Return None if found any ocr package need install before continue. 
    ocr_pkg_name = LANGUAGE_OCR_DICT[src_lang]
    pkg_names = get_install_packages([ocr_pkg_name])
    if len(pkg_names):
        show_message(_("An OCR package is required to enable word recognition"), _("Cancel"), _("Install"), lambda : install_packages(pkg_names))
        return None
    
    # Return None if mouse at trayicon area.
    if constant.TRAYAREA_TOP < mouse_y < constant.TRAYAREA_BOTTOM:
        return None

    # Ocr word under cursor.
    lang = ocr_pkg_name.split("tesseract-ocr-")[1].replace("-", "_")
    x = max(mouse_x - screenshot_width / 2, 0) 
    y = max(mouse_y - screenshot_height / 2, 0)
    width = min(mouse_x + screenshot_width / 2, screen_width) - x
    height = min(mouse_y + screenshot_height / 2, screen_height) - y
                    
    scale = 2
    tool = pyocr.get_available_tools()[0]
        
    output_format = xcb.xproto.ImageFormat.ZPixmap
    plane_mask = 2**32 - 1
    
    reply = conn.core.GetImage(
        output_format, 
        root, 
        x,
        y,
        width,
        height,
        plane_mask).reply()
    
    # Get screenshot image data.
    image_data = reply.data.buf()
    image = Image.frombuffer("RGBX", (width, height), image_data, "raw", "BGRX").convert("RGB")
    
    # First make image grey and scale bigger.
    image = image.convert("L").resize((width * scale, height * scale))
    
    # image.save("old.png")       # debug
    # Second enhance image with contrast and sharpness.
    image = ImageEnhance.Contrast(image).enhance(1.5)
    
    # I found uncomment below code have better result. ;)
    # image = ImageEnhance.Sharpness(image).enhance(2.0)
    
    # image.save("new.png")       # debug
    
    word_boxes = tool.image_to_string(
        image,
        lang=lang,
        builder=pyocr.builders.WordBoxBuilder())
    
    cursor_x = (mouse_x - x) * scale
    cursor_y = (mouse_y - y) * scale
    
    for word_box in word_boxes[::-1]:
        ((left_x, left_y), (right_x, right_y)) = word_box.position
        if (left_x <= cursor_x <= right_x and left_y <= cursor_y <= right_y):
            word = filter_punctuation(word_box.content)
            # Return None if ocr word is space string.
            if word.isspace():
                return None
            else:
                return word
        
    return None