Skip to content

Commit 74cd888

Browse files
authored
Merge pull request #30 from moheladwy/create-aur-package
added aur packaging + adjusted the scripts to meet the new setup way
2 parents b0dd0cc + 80dc25b commit 74cd888

7 files changed

Lines changed: 198 additions & 82 deletions

File tree

.SRCINFO

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pkgbase = ocr4linux-git
2+
pkgdesc = OCR CLI Tool for Extracting Text from Screenshots using bash and python
3+
pkgver = 1.4.2
4+
pkgrel = 1
5+
url = https://github.com/moheladwy/OCR4Linux
6+
arch = any
7+
license = MIT
8+
makedepends = git
9+
depends = python
10+
depends = bash
11+
depends = tesseract
12+
depends = gawk
13+
depends = tesseract-data-eng
14+
depends = tesseract-data-ara
15+
depends = python-numpy
16+
depends = python-pillow
17+
depends = python-pytesseract
18+
depends = python-opencv
19+
depends = grimblast-git
20+
depends = wl-clipboard
21+
depends = cliphist
22+
depends = xclip
23+
depends = scrot
24+
depends = rofi
25+
source = ocr4linux-git-1.4.2::git+https://github.com/moheladwy/OCR4Linux
26+
sha256sums = SKIP
27+
28+
pkgname = ocr4linux-git

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ __pycache__/
66
# C extensions
77
*.so
88

9+
# Arch Linux build
10+
pkg/
11+
src/
12+
*-any.pkg.*
13+
*.tar.gz
14+
915
# Distribution / packaging
1016
.Python
1117
build/
@@ -175,4 +181,4 @@ output*.txt
175181
#Ignore vscode AI rules
176182
.github/instructions/codacy.instructions.md
177183
.github/plans/
178-
.codacy/
184+
.codacy/

OCR4Linux.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ========================================================================================================================
22
# Author:
33
# Mohamed Hussein Al-Adawy
4-
# Version: 1.4.1
4+
# Version: 1.4.2
55
# Description:
66
# OCR4Linux.py is a Python script that handles image preprocessing and text extraction using Tesseract OCR.
77
# The script takes an input image, processes it for optimal OCR accuracy, and extracts text while preserving
@@ -152,7 +152,7 @@ def __init__(self):
152152
self.author = "Mohamed Hussein Al-Adawy"
153153
self.email = "mohamed.h.eladwy@gmail.com"
154154
self.github = "https://github.com/moheladwy/OCR4Linux"
155-
self.version = "1.4.1"
155+
self.version = "1.4.2"
156156
self.description = \
157157
" OCR4Linux.py is a Python script that handles image preprocessing\n" + \
158158
" and text extraction using Tesseract OCR. The script takes an input\n" + \

OCR4Linux.sh

Lines changed: 85 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
# ========================================================================================================================
33
# Author: Mohamed Hussein Al-Adawy
4-
# Version: 1.4.1
4+
# Version: 1.4.2
55
# Description:
66
# OCR4Linux is a versatile text extraction tool for Linux systems that:
77
# 1. Takes screenshots of selected areas using:
@@ -33,10 +33,11 @@
3333

3434
SCREENSHOT_NAME="screenshot_$(date +%d%m%Y_%H%M%S).jpg"
3535
SCREENSHOT_DIRECTORY="$HOME/Pictures/screenshots"
36-
OCR4Linux_HOME="$HOME/.config/OCR4Linux"
36+
OCR4Linux_HOME="$(pwd)"
3737
OCR4Linux_PYTHON_NAME="OCR4Linux.py"
38-
TEXT_OUTPUT_FILE_NAME="output_text.txt"
39-
LOGS_FILE_NAME="OCR4Linux.log"
38+
OCR4Linux_CONFIG="$HOME/.config/OCR4Linux"
39+
TEXT_OUTPUT_FILE_NAME="$OCR4Linux_CONFIG/output_text.txt"
40+
LOGS_FILE_NAME="$OCR4Linux_CONFIG/OCR4Linux.log"
4041
SLEEP_DURATION=0.5
4142
REMOVE_SCREENSHOT=false
4243
KEEP_LOGS=false
@@ -53,88 +54,69 @@ log_message() {
5354
if [ "$KEEP_LOGS" = true ]; then
5455
{
5556
echo "$message"
56-
} >>"$OCR4Linux_HOME/$LOGS_FILE_NAME"
57+
} >>"$LOGS_FILE_NAME"
5758
fi
5859
}
5960

60-
# Perform update by running setup.sh from source directory
61-
perform_update() {
62-
echo "To perform a full update, please git pull the latest version from the GitHub repository:"
63-
echo "1) cd ~/src/OCR4Linux # If you haven't cloned it yet, run: git clone https://github.com/moheladwy/OCR4Linux.git ~/src/OCR4Linux"
64-
echo "2) git pull"
65-
echo "3) Then run the setup script:"
66-
echo "4) chmod +x ./setup.sh"
67-
echo "5) ./setup.sh"
68-
echo "The setup script will update the OCR4Linux scripts and dependencies to the latest version."
69-
}
70-
7161
# Display help message
7262
show_help() {
7363
echo "Usage: $(basename "$0") [OPTIONS]"
7464
echo "Options:"
7565
echo " -r Remove screenshot in the screenshot directory"
7666
echo " -d DIRECTORY Set screenshot directory (default: $SCREENSHOT_DIRECTORY)"
7767
echo " -l Keep logs"
78-
echo " -u, --update Update OCR4Linux (scripts and dependencies)"
7968
echo " --lang LANGUAGES Specify OCR languages (e.g., 'all', 'eng', 'eng+ara')"
8069
echo " -h Show this help message, then exit"
8170
echo "Example:"
8271
echo " OCR4Linux.sh -d $HOME/screenshots -l"
8372
echo " OCR4Linux.sh --lang eng+ara"
8473
echo " OCR4Linux.sh --lang all -l"
85-
echo " OCR4Linux.sh -u"
86-
echo " OCR4Linux.sh --update"
8774
echo " OCR4Linux.sh -h"
8875
echo "Note:"
8976
echo " - If --lang is not specified, an interactive language selection menu will appear"
9077
echo " - Use 'all' to select all available languages"
9178
echo " - Use '+' to separate multiple languages (e.g., 'eng+ara+fra')"
9279
echo " - Without arguments, screenshots are saved to $SCREENSHOT_DIRECTORY"
93-
echo " - The -u or --update option gives instructions on how to update OCR4Linux."
9480
}
9581

9682
# Parse command line arguments
9783
while [[ $# -gt 0 ]]; do
9884
case $1 in
99-
-r)
100-
REMOVE_SCREENSHOT=true
101-
shift
102-
;;
103-
-d)
104-
SCREENSHOT_DIRECTORY="$2"
105-
shift 2
106-
;;
107-
-l)
108-
KEEP_LOGS=true
109-
shift
110-
;;
111-
-u|--update)
112-
perform_update
113-
exit 0
114-
;;
115-
--lang)
116-
SPECIFIED_LANGS="$2"
117-
LANG_SPECIFIED=true
118-
shift 2
119-
;;
120-
-h)
121-
show_help
122-
exit 0
123-
;;
124-
*)
125-
echo "Unknown option: $1"
126-
show_help
127-
exit 1
128-
;;
85+
-r)
86+
REMOVE_SCREENSHOT=true
87+
shift
88+
;;
89+
-d)
90+
SCREENSHOT_DIRECTORY="$2"
91+
shift 2
92+
;;
93+
-l)
94+
KEEP_LOGS=true
95+
shift
96+
;;
97+
--lang)
98+
SPECIFIED_LANGS="$2"
99+
LANG_SPECIFIED=true
100+
shift 2
101+
;;
102+
-h)
103+
show_help
104+
exit 0
105+
;;
106+
*)
107+
echo "Unknown option: $1"
108+
show_help
109+
exit 1
110+
;;
129111
esac
130112
done
131113

132-
# Check if the required files exist.
133-
check_if_files_exist() {
114+
# Check if the required files/binaries exist.
115+
check_if_requirements_exists() {
134116
log_message "Checking required files and directories..."
135117

136118
# Check if rofi is installed
137-
if ! command -v rofi &> /dev/null; then
119+
if ! command -v rofi &>/dev/null; then
138120
log_message "ERROR: rofi is not installed. Please install rofi to use language selection."
139121
exit 1
140122
fi
@@ -160,21 +142,43 @@ check_if_files_exist() {
160142
log_message "ERROR: $OCR4Linux_PYTHON_NAME not found in $OCR4Linux_HOME"
161143
exit 1
162144
fi
145+
146+
# Validate config directory
147+
if [ ! -d "$OCR4Linux_CONFIG" ]; then
148+
log_message "Creating config directory: $OCR4Linux_CONFIG since it does not exist."
149+
if ! mkdir -p "$OCR4Linux_CONFIG"; then
150+
log_message "ERROR: Failed to create directory $OCR4Linux_CONFIG"
151+
exit 1
152+
fi
153+
log_message "Successfully created config directory: $OCR4Linux_CONFIG"
154+
fi
155+
156+
# Check if the directory is writable
157+
if [ ! -w "$OCR4Linux_CONFIG" ]; then
158+
log_message "ERROR: $OCR4Linux_CONFIG is not writable"
159+
exit 1
160+
fi
163161
}
164162

165163
# Process specified languages from command line
166164
process_specified_langs() {
167165
log_message "Processing specified languages: $SPECIFIED_LANGS"
168-
166+
169167
# Handle "all" case
170168
if [[ "$SPECIFIED_LANGS" == "all" ]]; then
171169
mapfile -t langs < <(tesseract --list-langs | awk 'FNR>1')
172-
log_message "Using ALL available languages: $(IFS=+ ; echo "${langs[*]}")"
170+
log_message "Using ALL available languages: $(
171+
IFS=+
172+
echo "${langs[*]}"
173+
)"
173174
else
174175
# Split the language string by '+' and populate the langs array
175-
IFS='+' read -ra langs <<< "$SPECIFIED_LANGS"
176-
log_message "Using specified languages: $(IFS=+ ; echo "${langs[*]}")"
177-
176+
IFS='+' read -ra langs <<<"$SPECIFIED_LANGS"
177+
log_message "Using specified languages: $(
178+
IFS=+
179+
echo "${langs[*]}"
180+
)"
181+
178182
# Validate that the specified languages are available
179183
available_langs=$(tesseract --list-langs | awk 'FNR>1')
180184
for lang in "${langs[@]}"; do
@@ -188,7 +192,7 @@ process_specified_langs() {
188192
# Choose languages for OCR using rofi
189193
choose_lang() {
190194
log_message "Fetching available languages for OCR selection..."
191-
195+
192196
# Get available languages and add "ALL" option at the beginning
193197
mapfile -t langs < <(tesseract --list-langs | awk 'BEGIN {print "ALL" } FNR>1' | rofi -dmenu -multi-select -p "Select OCR Languages:")
194198

@@ -200,9 +204,15 @@ choose_lang() {
200204
# If "ALL" is selected, use all available languages
201205
if [[ " ${langs[*]} " =~ " ALL " ]]; then
202206
mapfile -t langs < <(tesseract --list-langs | awk 'FNR>1')
203-
log_message "Selected ALL languages: $(IFS=+ ; echo "${langs[*]}")"
207+
log_message "Selected ALL languages: $(
208+
IFS=+
209+
echo "${langs[*]}"
210+
)"
204211
else
205-
log_message "Selected languages: $(IFS=+ ; echo "${langs[*]}")"
212+
log_message "Selected languages: $(
213+
IFS=+
214+
echo "${langs[*]}"
215+
)"
206216
fi
207217
}
208218

@@ -236,35 +246,38 @@ extract_text() {
236246
# Create language string for passing to Python script
237247
local lang_string=""
238248
if [ ${#langs[@]} -gt 0 ]; then
239-
lang_string=$(IFS=+; echo "${langs[*]}")
249+
lang_string=$(
250+
IFS=+
251+
echo "${langs[*]}"
252+
)
240253
fi
241-
254+
242255
if [ -n "$lang_string" ]; then
243256
python "$OCR4Linux_HOME/$OCR4Linux_PYTHON_NAME" \
244257
"$SCREENSHOT_DIRECTORY/$SCREENSHOT_NAME" \
245-
"$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME" \
258+
"$TEXT_OUTPUT_FILE_NAME" \
246259
--langs "$lang_string"
247260
else
248261
python "$OCR4Linux_HOME/$OCR4Linux_PYTHON_NAME" \
249262
"$SCREENSHOT_DIRECTORY/$SCREENSHOT_NAME" \
250-
"$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME"
263+
"$TEXT_OUTPUT_FILE_NAME"
251264
fi
252265
log_message "Text extraction completed successfully"
253266
}
254267

255268
# Copy the extracted text to clipboard using wl-copy and cliphist.
256269
copy_to_wayland_clipboard() {
257270
log_message "Copying extracted text to Wayland clipboard using wl-copy and cliphist..."
258-
cliphist store <"$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME"
271+
cliphist store <"$TEXT_OUTPUT_FILE_NAME"
259272
cliphist list | head -n 1 | cliphist decode | wl-copy
260273
log_message "Extracted text copied to Wayland clipboard successfully."
261274
}
262275

263276
# Copy the extracted text to clipboard using xclip.
264277
copy_to_x11_clipboard() {
265278
log_message "Copying extracted text to X11 clipboard using xclip..."
266-
xclip -selection clipboard -i "$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME"
267-
xclip -selection primary -i "$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME"
279+
xclip -selection clipboard -i "$TEXT_OUTPUT_FILE_NAME"
280+
xclip -selection primary -i "$TEXT_OUTPUT_FILE_NAME"
268281
log_message "Extracted text copied to X11 clipboard successfully."
269282
}
270283

@@ -275,7 +288,7 @@ run_copy_to_clipboard() {
275288
else
276289
copy_to_x11_clipboard
277290
fi
278-
rm "$OCR4Linux_HOME/$TEXT_OUTPUT_FILE_NAME"
291+
rm "$TEXT_OUTPUT_FILE_NAME"
279292
log_message "The extracted text has been copied to the clipboard."
280293
}
281294

@@ -289,15 +302,15 @@ remove_image() {
289302

290303
# Run the functions
291304
main() {
292-
check_if_files_exist
293-
305+
check_if_requirements_exists
306+
294307
# Handle language selection
295308
if [ "$LANG_SPECIFIED" = true ]; then
296309
process_specified_langs
297310
else
298311
choose_lang
299312
fi
300-
313+
301314
takescreenshot
302315
extract_text
303316
run_copy_to_clipboard

0 commit comments

Comments
 (0)