From c34cee4e1b235a072cc77d7fa861ea1beb5c9406 Mon Sep 17 00:00:00 2001 From: moheladwy Date: Sat, 21 Mar 2026 14:49:52 +0200 Subject: [PATCH 1/4] added aur packaging + adjusted the scripts to meet the new setup way --- .gitignore | 8 ++- OCR4Linux.py | 4 +- OCR4Linux.sh | 141 +++++++++++++++++++++++++-------------------------- PKGBUILD | 53 +++++++++++++++++++ README.md | 2 +- setup.sh | 28 +++++++--- 6 files changed, 154 insertions(+), 82 deletions(-) create mode 100644 PKGBUILD diff --git a/.gitignore b/.gitignore index df4affb..f542f17 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,12 @@ __pycache__/ # C extensions *.so +# Arch Linux build +pkg/ +src/ +*-any.pkg.* +*.tar.gz + # Distribution / packaging .Python build/ @@ -175,4 +181,4 @@ output*.txt #Ignore vscode AI rules .github/instructions/codacy.instructions.md .github/plans/ -.codacy/ \ No newline at end of file +.codacy/ diff --git a/OCR4Linux.py b/OCR4Linux.py index bd9b343..0c05ab5 100644 --- a/OCR4Linux.py +++ b/OCR4Linux.py @@ -1,7 +1,7 @@ # ======================================================================================================================== # Author: # Mohamed Hussein Al-Adawy -# Version: 1.4.1 +# Version: 1.4.2 # Description: # OCR4Linux.py is a Python script that handles image preprocessing and text extraction using Tesseract OCR. # The script takes an input image, processes it for optimal OCR accuracy, and extracts text while preserving @@ -152,7 +152,7 @@ def __init__(self): self.author = "Mohamed Hussein Al-Adawy" self.email = "mohamed.h.eladwy@gmail.com" self.github = "https://github.com/moheladwy/OCR4Linux" - self.version = "1.4.1" + self.version = "1.4.2" self.description = \ " OCR4Linux.py is a Python script that handles image preprocessing\n" + \ " and text extraction using Tesseract OCR. The script takes an input\n" + \ diff --git a/OCR4Linux.sh b/OCR4Linux.sh index c25bfb6..86c564f 100755 --- a/OCR4Linux.sh +++ b/OCR4Linux.sh @@ -1,7 +1,7 @@ #!/bin/bash # ======================================================================================================================== # Author: Mohamed Hussein Al-Adawy -# Version: 1.4.1 +# Version: 1.4.2 # Description: # OCR4Linux is a versatile text extraction tool for Linux systems that: # 1. Takes screenshots of selected areas using: @@ -33,10 +33,11 @@ SCREENSHOT_NAME="screenshot_$(date +%d%m%Y_%H%M%S).jpg" SCREENSHOT_DIRECTORY="$HOME/Pictures/screenshots" -OCR4Linux_HOME="$HOME/.config/OCR4Linux" +OCR4Linux_HOME="$(pwd)" OCR4Linux_PYTHON_NAME="OCR4Linux.py" -TEXT_OUTPUT_FILE_NAME="output_text.txt" -LOGS_FILE_NAME="OCR4Linux.log" +OCR4Linux_CONFIG="$HOME/.config/OCR4Linux" +TEXT_OUTPUT_FILE_NAME="$OCR4Linux_CONFIG/output_text.txt" +LOGS_FILE_NAME="$OCR4Linux_CONFIG/OCR4Linux.log" SLEEP_DURATION=0.5 REMOVE_SCREENSHOT=false KEEP_LOGS=false @@ -53,21 +54,10 @@ log_message() { if [ "$KEEP_LOGS" = true ]; then { echo "$message" - } >>"$OCR4Linux_HOME/$LOGS_FILE_NAME" + } >>"$LOGS_FILE_NAME" fi } -# Perform update by running setup.sh from source directory -perform_update() { - echo "To perform a full update, please git pull the latest version from the GitHub repository:" - echo "1) cd ~/src/OCR4Linux # If you haven't cloned it yet, run: git clone https://github.com/moheladwy/OCR4Linux.git ~/src/OCR4Linux" - echo "2) git pull" - echo "3) Then run the setup script:" - echo "4) chmod +x ./setup.sh" - echo "5) ./setup.sh" - echo "The setup script will update the OCR4Linux scripts and dependencies to the latest version." -} - # Display help message show_help() { echo "Usage: $(basename "$0") [OPTIONS]" @@ -75,66 +65,58 @@ show_help() { echo " -r Remove screenshot in the screenshot directory" echo " -d DIRECTORY Set screenshot directory (default: $SCREENSHOT_DIRECTORY)" echo " -l Keep logs" - echo " -u, --update Update OCR4Linux (scripts and dependencies)" echo " --lang LANGUAGES Specify OCR languages (e.g., 'all', 'eng', 'eng+ara')" echo " -h Show this help message, then exit" echo "Example:" echo " OCR4Linux.sh -d $HOME/screenshots -l" echo " OCR4Linux.sh --lang eng+ara" echo " OCR4Linux.sh --lang all -l" - echo " OCR4Linux.sh -u" - echo " OCR4Linux.sh --update" echo " OCR4Linux.sh -h" echo "Note:" echo " - If --lang is not specified, an interactive language selection menu will appear" echo " - Use 'all' to select all available languages" echo " - Use '+' to separate multiple languages (e.g., 'eng+ara+fra')" echo " - Without arguments, screenshots are saved to $SCREENSHOT_DIRECTORY" - echo " - The -u or --update option gives instructions on how to update OCR4Linux." } # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in - -r) - REMOVE_SCREENSHOT=true - shift - ;; - -d) - SCREENSHOT_DIRECTORY="$2" - shift 2 - ;; - -l) - KEEP_LOGS=true - shift - ;; - -u|--update) - perform_update - exit 0 - ;; - --lang) - SPECIFIED_LANGS="$2" - LANG_SPECIFIED=true - shift 2 - ;; - -h) - show_help - exit 0 - ;; - *) - echo "Unknown option: $1" - show_help - exit 1 - ;; + -r) + REMOVE_SCREENSHOT=true + shift + ;; + -d) + SCREENSHOT_DIRECTORY="$2" + shift 2 + ;; + -l) + KEEP_LOGS=true + shift + ;; + --lang) + SPECIFIED_LANGS="$2" + LANG_SPECIFIED=true + shift 2 + ;; + -h) + show_help + exit 0 + ;; + *) + echo "Unknown option: $1" + show_help + exit 1 + ;; esac done -# Check if the required files exist. -check_if_files_exist() { +# Check if the required files/binaries exist. +check_if_requirements_exists() { log_message "Checking required files and directories..." # Check if rofi is installed - if ! command -v rofi &> /dev/null; then + if ! command -v rofi &>/dev/null; then log_message "ERROR: rofi is not installed. Please install rofi to use language selection." exit 1 fi @@ -165,16 +147,22 @@ check_if_files_exist() { # Process specified languages from command line process_specified_langs() { log_message "Processing specified languages: $SPECIFIED_LANGS" - + # Handle "all" case if [[ "$SPECIFIED_LANGS" == "all" ]]; then mapfile -t langs < <(tesseract --list-langs | awk 'FNR>1') - log_message "Using ALL available languages: $(IFS=+ ; echo "${langs[*]}")" + log_message "Using ALL available languages: $( + IFS=+ + echo "${langs[*]}" + )" else # Split the language string by '+' and populate the langs array - IFS='+' read -ra langs <<< "$SPECIFIED_LANGS" - log_message "Using specified languages: $(IFS=+ ; echo "${langs[*]}")" - + IFS='+' read -ra langs <<<"$SPECIFIED_LANGS" + log_message "Using specified languages: $( + IFS=+ + echo "${langs[*]}" + )" + # Validate that the specified languages are available available_langs=$(tesseract --list-langs | awk 'FNR>1') for lang in "${langs[@]}"; do @@ -188,7 +176,7 @@ process_specified_langs() { # Choose languages for OCR using rofi choose_lang() { log_message "Fetching available languages for OCR selection..." - + # Get available languages and add "ALL" option at the beginning mapfile -t langs < <(tesseract --list-langs | awk 'BEGIN {print "ALL" } FNR>1' | rofi -dmenu -multi-select -p "Select OCR Languages:") @@ -200,9 +188,15 @@ choose_lang() { # If "ALL" is selected, use all available languages if [[ " ${langs[*]} " =~ " ALL " ]]; then mapfile -t langs < <(tesseract --list-langs | awk 'FNR>1') - log_message "Selected ALL languages: $(IFS=+ ; echo "${langs[*]}")" + log_message "Selected ALL languages: $( + IFS=+ + echo "${langs[*]}" + )" else - log_message "Selected languages: $(IFS=+ ; echo "${langs[*]}")" + log_message "Selected languages: $( + IFS=+ + echo "${langs[*]}" + )" fi } @@ -236,18 +230,21 @@ extract_text() { # Create language string for passing to Python script local lang_string="" if [ ${#langs[@]} -gt 0 ]; then - lang_string=$(IFS=+; echo "${langs[*]}") + lang_string=$( + IFS=+ + echo "${langs[*]}" + ) fi - + if [ -n "$lang_string" ]; then python "$OCR4Linux_HOME/$OCR4Linux_PYTHON_NAME" \ "$SCREENSHOT_DIRECTORY/$SCREENSHOT_NAME" \ - "$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME" \ + "$TEXT_OUTPUT_FILE_NAME" \ --langs "$lang_string" else python "$OCR4Linux_HOME/$OCR4Linux_PYTHON_NAME" \ "$SCREENSHOT_DIRECTORY/$SCREENSHOT_NAME" \ - "$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME" + "$TEXT_OUTPUT_FILE_NAME" fi log_message "Text extraction completed successfully" } @@ -255,7 +252,7 @@ extract_text() { # Copy the extracted text to clipboard using wl-copy and cliphist. copy_to_wayland_clipboard() { log_message "Copying extracted text to Wayland clipboard using wl-copy and cliphist..." - cliphist store <"$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME" + cliphist store <"$TEXT_OUTPUT_FILE_NAME" cliphist list | head -n 1 | cliphist decode | wl-copy log_message "Extracted text copied to Wayland clipboard successfully." } @@ -263,8 +260,8 @@ copy_to_wayland_clipboard() { # Copy the extracted text to clipboard using xclip. copy_to_x11_clipboard() { log_message "Copying extracted text to X11 clipboard using xclip..." - xclip -selection clipboard -i "$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME" - xclip -selection primary -i "$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME" + xclip -selection clipboard -i "$TEXT_OUTPUT_FILE_NAME" + xclip -selection primary -i "$TEXT_OUTPUT_FILE_NAME" log_message "Extracted text copied to X11 clipboard successfully." } @@ -275,7 +272,7 @@ run_copy_to_clipboard() { else copy_to_x11_clipboard fi - rm "$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME" + rm "$TEXT_OUTPUT_FILE_NAME" log_message "The extracted text has been copied to the clipboard." } @@ -289,15 +286,15 @@ remove_image() { # Run the functions main() { - check_if_files_exist - + check_if_requirements_exists + # Handle language selection if [ "$LANG_SPECIFIED" = true ]; then process_specified_langs else choose_lang fi - + takescreenshot extract_text run_copy_to_clipboard diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..6c308ce --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,53 @@ +# Maintainer: moheladwy +pkgname=ocr4linux +pkgver=1.4.2 +pkgrel=1 +pkgdesc="OCR Script CLI Tool for Extracting Text from Screenshots using bash and python" +arch=('any') +url="https://github.com/moheladwy/OCR4Linux" +license=('MIT') +depends=( + 'python' + 'bash' + 'tesseract' + 'gawk' + 'tesseract-data-eng' + 'tesseract-data-ara' + 'python-numpy' + 'python-pillow' + 'python-pytesseract' + 'python-opencv' + 'grimblast-git' + 'wl-clipboard' + 'cliphist' + 'xclip' + 'scrot' + 'rofi' +) +makedepends=('git') +#source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz") +source=("$pkgname-$pkgver::git+$url") +sha256sums=('SKIP') + +package() { + # Find the extracted directory dynamically (handles case-sensitivity differences) + local extracted_dir + extracted_dir=$(cd "$srcdir" && ls -d * -d *-"${pkgver}" 2>/dev/null | grep -E "(OCR|ocr).*${pkgver}" | head -1) + + if [ -z "$extracted_dir" ]; then + echo "Error: Could not find extracted source directory in $srcdir" + exit 1 + fi + + cd "$srcdir/$extracted_dir" || exit 1 + + # Install shell and Python scripts + install -Dm755 OCR4Linux.py "${pkgdir}/usr/bin/OCR4Linux.py" + install -Dm755 OCR4Linux.sh "${pkgdir}/usr/bin/OCR4Linux" + + # Install documentation + install -Dm644 README.md "${pkgdir}/usr/share/doc/${pkgname}/README.md" + + # Install License + install -Dm644 LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}" +} diff --git a/README.md b/README.md index 6544e96..29a6050 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # OCR4Linux -**Version:** 1.4.1 +**Version:** 1.4.2 OCR4Linux is a versatile text extraction tool that allows you to take a screenshot of a selected area, extract text using OCR, and copy it to the clipboard. It supports both Wayland and X11 sessions and offers multiple language support. diff --git a/setup.sh b/setup.sh index 4e438cd..7fcb366 100755 --- a/setup.sh +++ b/setup.sh @@ -2,14 +2,14 @@ # ======================================================================================================================== # Author: # Mohamed Hussein Al-Adawy -# Version: 1.4.1 +# Version: 1.4.2 # Description: # This setup script installs and configures OCR4Linux and its dependencies. # It handles the installation of: -# 1. System requirements (tesseract, python packages) +# 1. System requirements (tesseract, python packages, rofi) # 2. Session-specific tools: -# - Wayland: grimblast, wl-clipboard, cliphist, rofi-wayland -# - X11: xclip, scrot, rofi +# - Wayland: grimblast, wl-clipboard, cliphist +# - X11: xclip, scrot # # Features: # - Automatic detection and installation of AUR helper (yay) @@ -38,17 +38,16 @@ sys_requirements=( python-pillow python-pytesseract python-opencv + rofi ) wayland_session_apps=( grimblast-git wl-clipboard cliphist - rofi-wayland ) x11_session_apps=( xclip scrot - rofi ) # Check if yay is installed. @@ -88,9 +87,26 @@ install_requirements() { } # Main function. +print_help() { + echo "OCR4Linux Setup Script" + echo "This script will manually install and configure OCR4Linux and its dependencies." + echo "Note: This script is designed for manual installations." + echo "For Arch-based Linux users, it is recommended to install OCR4Linux using the AUR package \"ocr4linux\"." +} + main() { echo "========================================================================================================" echo "Starting OCR4Linux setup..." + echo "========================================================================================================" + print_help + + # Ask the user whether to proceed with manual installation + read -r -p "Do you want to continue with the manual installation? (y/n): " confirm + if [[ ! $confirm =~ ^[Yy]$ ]]; then + echo "Aborting setup. Use the AUR package 'ocr4linux' for Arch-based systems." + exit 0 + fi + echo "========================================================================================================" check_yay echo "========================================================================================================" From 1c223a7f8e1be98629f14f3f070cc8e578c5c847 Mon Sep 17 00:00:00 2001 From: moheladwy Date: Sat, 21 Mar 2026 15:00:05 +0200 Subject: [PATCH 2/4] update the package name to have -git at the end --- PKGBUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PKGBUILD b/PKGBUILD index 6c308ce..0aa84fd 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,5 +1,5 @@ # Maintainer: moheladwy -pkgname=ocr4linux +pkgname=ocr4linux-git pkgver=1.4.2 pkgrel=1 pkgdesc="OCR Script CLI Tool for Extracting Text from Screenshots using bash and python" From 9d4da613b1e131560cc7da4eda1e185dc40a1dcd Mon Sep 17 00:00:00 2001 From: moheladwy Date: Sat, 21 Mar 2026 15:08:36 +0200 Subject: [PATCH 3/4] update the script to validate and create the config file --- OCR4Linux.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/OCR4Linux.sh b/OCR4Linux.sh index 86c564f..0399f88 100755 --- a/OCR4Linux.sh +++ b/OCR4Linux.sh @@ -142,6 +142,22 @@ check_if_requirements_exists() { log_message "ERROR: $OCR4Linux_PYTHON_NAME not found in $OCR4Linux_HOME" exit 1 fi + + # Validate config directory + if [ ! -d "$OCR4Linux_CONFIG" ]; then + log_message "Creating config directory: $OCR4Linux_CONFIG since it does not exist." + if ! mkdir -p "$OCR4Linux_CONFIG"; then + log_message "ERROR: Failed to create directory $OCR4Linux_CONFIG" + exit 1 + fi + log_message "Successfully created config directory: $OCR4Linux_CONFIG" + fi + + # Check if the directory is writable + if [ ! -w "$OCR4Linux_CONFIG" ]; then + log_message "ERROR: $OCR4Linux_CONFIG is not writable" + exit 1 + fi } # Process specified languages from command line From 80dc25b6c8ed1058fe2e4ffabba063ab5370f468 Mon Sep 17 00:00:00 2001 From: moheladwy Date: Sat, 21 Mar 2026 15:43:26 +0200 Subject: [PATCH 4/4] added the .SRCINFO file --- .SRCINFO | 28 ++++++++++++++++++++++++++++ PKGBUILD | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 .SRCINFO diff --git a/.SRCINFO b/.SRCINFO new file mode 100644 index 0000000..015a81a --- /dev/null +++ b/.SRCINFO @@ -0,0 +1,28 @@ +pkgbase = ocr4linux-git + pkgdesc = OCR CLI Tool for Extracting Text from Screenshots using bash and python + pkgver = 1.4.2 + pkgrel = 1 + url = https://github.com/moheladwy/OCR4Linux + arch = any + license = MIT + makedepends = git + depends = python + depends = bash + depends = tesseract + depends = gawk + depends = tesseract-data-eng + depends = tesseract-data-ara + depends = python-numpy + depends = python-pillow + depends = python-pytesseract + depends = python-opencv + depends = grimblast-git + depends = wl-clipboard + depends = cliphist + depends = xclip + depends = scrot + depends = rofi + source = ocr4linux-git-1.4.2::git+https://github.com/moheladwy/OCR4Linux + sha256sums = SKIP + +pkgname = ocr4linux-git diff --git a/PKGBUILD b/PKGBUILD index 0aa84fd..84ac0de 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -2,7 +2,7 @@ pkgname=ocr4linux-git pkgver=1.4.2 pkgrel=1 -pkgdesc="OCR Script CLI Tool for Extracting Text from Screenshots using bash and python" +pkgdesc="OCR CLI Tool for Extracting Text from Screenshots using bash and python" arch=('any') url="https://github.com/moheladwy/OCR4Linux" license=('MIT')