Skip to content

Instantly share code, notes, and snippets.

@LaptopDev
Last active November 5, 2023 17:21

Revisions

  1. LaptopDev revised this gist Nov 5, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ocr.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Tesseract OCR ; process images as arguments to convert image to text

    ## .bashrc:
    # alias catpic='python3 /home/user/system-bash-scripts/ocr.py'
    # alias catpic='python3 $home/scripts/ocr.py'

    ## keybind that I use in my hyprland configuration file:
    # bind = SUPER, o, exec, grim -g "$(slurp)" - | python3 $home/scripts/ocr.py | wl-copy
  2. LaptopDev created this gist Nov 5, 2023.
    52 changes: 52 additions & 0 deletions ocr.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    # Tesseract OCR ; process images as arguments to convert image to text

    ## .bashrc:
    # alias catpic='python3 /home/user/system-bash-scripts/ocr.py'

    ## keybind that I use in my hyprland configuration file:
    # bind = SUPER, o, exec, grim -g "$(slurp)" - | python3 $home/scripts/ocr.py | wl-copy



    #!/usr/bin/env python3

    import sys
    import os
    import argparse
    import pytesseract
    from PIL import Image
    import io

    # Use Tesseract to do OCR on the image
    # Strip the trailing newlines and whitespace from the text

    def ocr_image(image):
    text = pytesseract.image_to_string(image)
    return text.rstrip()

    # Set up argument parser
    parser = argparse.ArgumentParser(description='Perform OCR on an image.')
    parser.add_argument('file', nargs='?', help='Image file to perform OCR on', type=argparse.FileType('rb'))

    # Parse arguments
    args = parser.parse_args()

    # Check if a file was provided as an argument
    if args.file:
    # Open the image file
    image = Image.open(args.file)
    text = ocr_image(image)
    else:
    # Read the image stream from stdin if no file argument is provided
    image_stream = io.BytesIO(sys.stdin.buffer.read())
    image = Image.open(image_stream)
    text = ocr_image(image)

    # Check if stdout is connected to a terminal
    if os.isatty(sys.stdout.fileno()):
    # We are in a terminal; print as usual
    print(text)
    else:
    # We are not in a terminal; avoid printing the extra newline
    print(text, end='')