Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ __pycache__/
# C extensions
*.so

# Arch Linux build
pkg/
src/
*-any.pkg.*
*.tar.gz
Comment thread
moheladwy marked this conversation as resolved.

# Distribution / packaging
.Python
build/
Expand Down Expand Up @@ -175,4 +181,4 @@ output*.txt
#Ignore vscode AI rules
.github/instructions/codacy.instructions.md
.github/plans/
.codacy/
.codacy/
4 changes: 2 additions & 2 deletions OCR4Linux.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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" + \
Expand Down
141 changes: 69 additions & 72 deletions OCR4Linux.sh
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)"
Comment thread
moheladwy marked this conversation as resolved.
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"
Comment thread
moheladwy marked this conversation as resolved.
SLEEP_DURATION=0.5
REMOVE_SCREENSHOT=false
KEEP_LOGS=false
Expand All @@ -53,88 +54,69 @@ 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]"
echo "Options:"
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
Comment thread
moheladwy marked this conversation as resolved.
;;
-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
Comment thread
moheladwy marked this conversation as resolved.
Expand Down Expand Up @@ -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
Expand All @@ -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:")

Expand All @@ -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
}

Expand Down Expand Up @@ -236,35 +230,38 @@ 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"
Comment thread
moheladwy marked this conversation as resolved.
}

# 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."
}

# 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."
}

Expand All @@ -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."
}

Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions PKGBUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Maintainer: moheladwy <mohamed.h.eladwy@gmail.com>
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')
Comment thread
moheladwy marked this conversation as resolved.

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}"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OCR4Linux

**Version:** 1.4.1
**Version:** 1.4.2
Comment thread
moheladwy marked this conversation as resolved.

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.

Expand Down
Loading
Loading